JavaScript - 解析UTC日期 [英] JavaScript - Parse UTC Date
问题描述
如何解析一个简单的日期字符串,让JavaScript知道它实际上是一个UTC日期?目前,如果我执行新日期('2015-08-27')
,则会将其转换为我的时区。
How can I parse a simple date string and let JavaScript know that it's actually a UTC date? Currently, if I do new Date('2015-08-27')
it converts it to my timezone.
推荐答案
您可以附加'T00:00:00.000Z'以使时区特定(Z表示UTC)
You can do append 'T00:00:00.000Z' to make the time zone specific (Z indicates UTC)
new Date('2015-08-27' + 'T00:00:00.000Z')
请注意,新日期('2015-08-27')在ES5(UTC)与ES6(本地)中的处理方式不同,因此您不能指望任何更正可以保持一致计划硬编码(即不要这样做)
Note that new Date('2015-08-27') is treated differently in ES5 (UTC) vs. ES6 (Local), so you can't expect it any correction to be work consistently if you were planning to to hard code it (i.e. don't do it)
另外,请注意你的 console.log
可能会显示与表达式求值的UTC时间相对应的本地时间(如果您希望UTC结束表达式,那么往往会让您失望一点评估到UTC时间和最后的当地时区,以评估当地时间。例如
Also, do note that your console.log
might show you the local time corresponding to the UTC time the expression evaluates to (that tends to throw you off a bit if you are expecting UTC to be at the end for expression that evaluate to UTC times and your local time zone at the end for those that evaluate to your local time). For instance
new Date('2015-08-27T00:00:00.000Z')
可以显示
Thu Aug 27 2015 1:00:00 GMT+100
这是相同
Thu Aug 27 2015 00:00:00 UTC
这篇关于JavaScript - 解析UTC日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!