在节点控制台中测试:
var moment = require('moment');
// create a new Date-Object
var now = new Date(2013, 02, 28, 11, 11, 11);
// create the native timestamp
var native = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds());
// create the timestamp with moment
var withMoment = moment.utc(now).valueOf()
// it doesnt matter if i use moment(now).utc().valueOf() or moment().utc(now).valueOf()
// native: 1364469071000
// withMoment: 1364465471000
native === withMoment // false!?!?!
// this returns true!!!
withMoment === now.getTime()
Run Code Online (Sandbox Code Playgroud)
为什么本机的时间戳与withMoment相同?为什么withMoment返回从当前本地时间计算的时间戳?我怎么能实现那个moment.utc()返回与Date.UTC()相同的?
我正在使用 moment.js 将 ISO 日期/时间/区域字符串转换为本地字符串。根据文档和其他类似的问题,例如this,本来应该非常简单的事情结果却并非如此,并且给了我一些奇怪的输出。这是我所拥有的:
\n\nconsole.log(\'date/time before is: \', date);\n// date/time before is: 2016-12-23T23:10:00.000Z\n\nvar datetime = moment(date).format("dddd, MMMM Do YYYY, h:mm:ss a");\n\nconsole.log(\'date/time after is: \', datetime);\n// date/time after is: p\xc3\xa1tek, prosinec 23. 2016, 3:10:00 pm\nRun Code Online (Sandbox Code Playgroud)\n\n我使用的格式字符串直接来自文档。目的是在它工作后能够按照我需要的方式对其进行格式化。
\n