【问题标题】:momentJS get UTC timestampmomentJS 获取 UTC 时间戳
【发布时间】:2015-01-08 09:57:06
【问题描述】:

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

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

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

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

如果我这样做

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

输出是

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

谢谢

【问题讨论】:

  • start的值是多少?
  • var start = Date.now();
  • 更新了问题:) 谢谢
  • 您在寻找时间戳 - 而不是格式化日期?然后使用.valueOf() 而不是.toDate()
  • 我正在寻找一个格式化的日期,显然它不起作用。

标签: node.js momentjs


【解决方案1】:

来自 cmets 的最简单答案:

moment.utc().valueOf()

提供 UTC 时间戳

【讨论】:

  • 或使用:moment.utc().format("X") - 以秒为单位的 uniх 时间戳
【解决方案2】:
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());

【讨论】:

  • 这不像问题中指定的那样工作。我的输出 console.log(moment.utc().toDate());是Tue Nov 11 2014 14:02:10 GMT-0500 (EST)
  • 我如何获取 UTC 的 JavaScript 日期??
  • 非常令人沮丧...这不起作用:var utc = moment.utc().valueOf(); console.log('moment.utc()',moment(utc).toDate());
  • 更新了我的答案。您可以使用格式来获​​取您要查找的内容。
  • 但我需要 .toDate() 将结果粘贴到 Mongo 中
猜你喜欢
  • 2014-03-31
  • 2011-12-24
  • 2012-02-28
  • 2014-11-21
  • 2012-05-29
  • 2017-10-07
  • 1970-01-01
  • 2012-04-03
  • 2019-05-02
相关资源
最近更新 更多