MomentJS toISOString的结果不同吗? [英] MomentJS 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?
谢谢.
推荐答案
文档说:
默认情况下,时刻会解析并以当地时间显示.
By default, moment parses and displays in local time.
而 .toISOString()
始终以UTC返回时间戳:
while .toISOString()
always returns a timestamp in UTC:
请注意,即使所讨论的时刻处于本地模式,
.toISOString()
始终以UTC返回时间戳.这样做是为了与本机JavaScript日期.toISOString()
的规范保持一致,如
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, String);
If your input string represents a UTC time, then use moment.utc(String, String);
这篇关于MomentJS toISOString的结果不同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!