我尝试获取 UTC 日期。为什么
(new Date("2015-04-22")).getUTCDate() = 22
还有
(new Date("Apr 22, 2015")).getUTCDate() = 21
?
最佳答案
引用 MDN 关于 Date.parse
的工作原理(由 new Date("string")
调用):
Differences in assumed time zone
Given a date string of
"March 7, 2014"
,parse()
assumes a local time zone, but given an ISO format such as"2014-03-07"
it will assume a time zone of UTC. Therefore Date objects produced using those strings will represent different moments in time unless the system is set with a local time zone of UTC. This means that two date strings that appear equivalent may result in two different values depending on the format of the string that is being converted (this behavior is changed in ECMAScript ed 6 so that both will be treated as local).
请注意我在最后一行突出显示的警告
关于JavaScript UTC 日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29798914/