node.js momentJS 获取 UTC 时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26871886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:06:30  来源:igfitidea点击:

momentJS get UTC timestamp

node.jsmomentjs

提问by Cmag

Folks, I am unable to get the UTC timestamp using the momentjs. Hope someone can point me in the right direction

伙计们,我无法使用 momentjs 获取 UTC 时间戳。希望有人能指出我正确的方向

var start = Date.now();
var utc = moment.utc(start).toDate();

or

或者

var utc = moment.utc().toDate();
Tue Nov 11 2014 13:45:13 GMT-0500 (EST)

Returns the EST timezone I am in, not UTC. How do i get the Javascript Date in UTC?

返回我所在的 EST 时区,而不是 UTC。我如何获得 UTC 中的 Javascript 日期?

If I do

如果我做

var utc= moment.utc();
console.log(utc);

output is

输出是

 { _useUTC: true,
  _isUTC: true,
  _l: undefined,
  _i: undefined,
  _f: undefined,
  _d: Tue Nov 11 2014 13:43:21 GMT-0500 (EST) }

Thanks

谢谢

回答by Charles Foster

Simplest answer from the comments :

评论中最简单的答案:

moment.utc().valueOf()

Gives UTC timestamp

给出 UTC 时间戳

回答by Daniel

var moment = require('moment');

// timestamp with UTC time
console.log(moment.utc().format('ddd MMM DD YYYY HH:mm:ss z'));

// or via the date object
console.log(moment.utc().toDate().toUTCString());