moment.js - UTC不能像我期望的那样工作 [英] moment.js - UTC does not work as i expect it

查看:112
本文介绍了moment.js - UTC不能像我期望的那样工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在节点控制台中进行测试:

testing in the node console:

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

为什么本机的时间戳与wit​​hMoment相同?为什么withMoment返回从当前本地时间计算的时间戳?我怎样才能实现那个时刻.utc()返回与Date.UTC()相同的颜色?

why isnt native the same timestamp as withMoment? why does withMoment return the timestamp calculated from the current local-time? how can i achieve that moment.utc() returns the same as Date.UTC()?

推荐答案

致电 moment.utc()与您调用的方式相同 Date.UTC

Call moment.utc() the same way you're calling Date.UTC:

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

我认为打电话给 moment.utc(现在)将假设现在存在于当地时区,它将首先将其转换为UTC,因此存在差异。

I think calling moment.utc(now) will make it assume now lives in the local timezone, and it will convert it to UTC first, hence the difference.

这篇关于moment.js - UTC不能像我期望的那样工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆