gpt4 book ai didi

Javascript Date() 传递日期和日期时间之间的差异

转载 作者:行者123 更新时间:2023-11-28 15:11:21 26 4
gpt4 key购买 nike

为什么当我传递日期时与传递日期和时间时,使用 new Date() 会得到 2 个不同的日期?

示例:

Date.parse('2015-03-14')
// 1426291200000
new Date(1426291200000)
// Fri Mar 13 2015 17:00:00 GMT-0700 (PDT)

Date.parse('2015-03-14 00:00:00')
// 1426316400000
new Date(1426291400000)
// Fri Mar 13 2015 17:03:20 GMT-0700 (PDT)

最佳答案

抱歉,我还不能发表评论。您在 Date.parse() 中传递的 string 必须遵循

ECMAScript 5 ISO-8601 format support .Reference link here

format specified in ECMAScript 2015

据此,日期和时间的格式必须是:'2015-03-14T00:00:00'而不是'2015-03-14 00:00: 00'

由于 Date.parse('2015-03-14') 的时间是 GMT+00,但 Date.parse('2015-03-14T00:00:00 ') 位于您的时区,因此如果您将时区 GMT+00 添加到第二个时间,则两个时间将相等:

Date.parse('2015-03-14');
Date.parse('2015-03-14T00:00:00+00:00');
//1426291200000

关于Javascript Date() 传递日期和日期时间之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36347796/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com