gpt4 book ai didi

javascript - moment.js - UTC 不能像我预期的那样工作

转载 作者:数据小太阳 更新时间:2023-10-29 04:06:21 24 4
gpt4 key购买 nike

在 Node 控制台中测试:

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()

为什么 native 的时间戳和 withMoment 不一样?为什么 withMoment 返回从当前本地时间计算的时间戳?我怎样才能实现 moment.utc() 返回与 Date.UTC() 相同的结果?

最佳答案

调用 moment.utc() 的方式与调用 Date.UTC 的方式相同:

var withMoment = moment.utc([now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds()]).valueOf();

我认为调用 moment.utc(now) 会使其假设 now 位于本地时区,并且它会首先将其转换为 UTC,因此存在差异。

关于javascript - moment.js - UTC 不能像我预期的那样工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15133326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com