Moment.js toISOString 结果不一样? [英] Moment.js toISOString result is different?
问题描述
代码有问题.
var mom = moment("23-11-2016 00:00", "DD-MM-YYYY HH:mm");
alert(mom.toISOString());
//result 2016-11-22T17:00:00.000Z
为什么结果不是2016-11-23T00:00:00.000Z
?我如何获得 2016-11-23T00:00:00.000Z
结果?
Why the result is not 2016-11-23T00:00:00.000Z
? How I can get 2016-11-23T00:00:00.000Z
result?
推荐答案
正如 doc 所说:
默认情况下,时刻以本地时间解析和显示.
By default, moment parses and displays in local time.
而 .toISOString()
总是以 UTC 格式返回时间戳:
while .toISOString()
always returns a timestamp in UTC:
请注意,.toISOString()
始终以 UTC 格式返回时间戳,即使相关时刻处于本地模式.这样做是为了与原生 JavaScript 日期 .toISOString()
的规范保持一致,如 ES2015 规范.
Note that
.toISOString()
always returns a timestamp in UTC, even if the moment in question is in local mode. This is done to provide consistency with the specification for native JavaScript Date.toISOString()
, as outlined in the ES2015 specification.
您可能与 UTC 有 -7 小时的偏差.
You probably have -7 hours offset from UTC.
如果你想使用 format()
以当地时间显示日期.
Use format()
if you want to display date in local time.
如果您的输入字符串表示 UTC 时间,则使用 moment.utc(String, 字符串);
If your input string represents a UTC time, then use moment.utc(String, String);
这篇关于Moment.js toISOString 结果不一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!