gpt4 book ai didi

javascript - 为什么这些日期不同?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:12:26 24 4
gpt4 key购买 nike

这里是控制台输出:

new Date(2016, 08, 22)
Thu Sep 22 2016 00:00:00 GMT+0200 (CEST)

new Date("2016, 08, 22")
Mon Aug 22 2016 00:00:00 GMT+0200 (CEST)

不同的月份,但为什么?

最佳答案

Javascript 月份在数字大小写中为 0,但在字符串解析中 08 映射到 August,因为它是标准日期格式的“August”的字符串翻译。Date 以不同的方式调用,如 here 所述

  1. 新日期();
  2. 新日期(值);
  3. 新日期(dateString);
  4. 新日期(年,月[,日[,小时[,分钟[,秒[,毫秒]]]]]);

在您的例子中,调用了 3 和 4 格式。

>> new Date(2016, 08, 22)
>> Thu Sep 22 2016 00:00:00 GMT+0530 (IST)
>> new Date(2016, 01, 22)
>> Mon Feb 22 2016 00:00:00 GMT+0530 (IST)
>> new Date(2016, 0, 22)
>> Fri Jan 22 2016 00:00:00 GMT+0530 (IST)
>> new Date("2016-08-22")
>> Mon Aug 22 2016 05:30:00 GMT+0530 (IST)
>> new Date("2016/08/22")
>> Mon Aug 22 2016 00:00:00 GMT+0530 (IST)
  • @RobG 来自评论的意见:

...parsing ofstrings other than ISO 8601 extended format is entirely implementation dependent. The result of parsing "2016, 08, 22" could be anything, including an invalid Date.

关于javascript - 为什么这些日期不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39074462/

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