问题描述
在PHP中,我可以使用date()函数,然后执行无论我想要什么...
JS / jQuery中是否有类似的东西?
令人惊讶的没有人发布关于moment.js,它与date.js非常相似,但更轻(5.5k最小化)
只是做了一个基准测试,发现它解析得更快而不是date.js,但是格式化和操作更慢。对于单线程,由于解析速度更快,它的执行速度要比date.js快。
使用示例:
//模拟ajax响应
var data = {someDate:2023-08-23 11: 52:39}
// .unix()转换为Unix时间戳:1692816759
moment(data.someDate).unix();
//以可读格式显示
// Aug 23,11:52 AM
moment(2023-08-23 11:52:39)。格式('MMM D,hh:mm A');
//现在
moment();
//支持操作和链接
moment()。add('days',7).subtract('months',1).hours(15).minutes(0) .seconds(0);
I'm attempting to calculate the day of the year (either with today or another date).
In PHP I can use the date() function and do whatever I want...
Is there something similar in JS/jQuery?
Surprised nobody posted about moment.js, it's very similar to date.js, but lighter (5.5k minified)
Just did a benchmark and found it parses much faster than date.js, but formatting and manipulating is slower. For one-liners it'd execute faster than date.js due to the faster parsing.
http://jsperf.com/underscore-date-vs-datejs/7
Some usage examples:
// Simulates ajax response
var data = { someDate: "2023-08-23 11:52:39" }
// .unix() converts to Unix timestamp: 1692816759
moment(data.someDate).unix();
// Displaying it in a readable format
// Aug 23, 11:52 AM
moment("2023-08-23 11:52:39").format('MMM D, hh:mm A');
// Now
moment();
// Support for manipulation and chaining
moment().add('days', 7).subtract('months', 1).hours(15).minutes(0).seconds(0);
这篇关于在javascript / jquery中是否有PHP date()等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!