gpt4 book ai didi

javascript - Date.parse 的不同行为?

转载 作者:行者123 更新时间:2023-12-02 20:32:35 25 4
gpt4 key购买 nike

alert(Date.parse('Mar 1 1990'));

在 jsFiddle 中,这将返回一个日期时间整数,如预期的那样。在我的机器上,它返回...时间戳字符串?

1990 年 3 月 1 日星期四 00:00:00 GMT-0500(EST)636267600000

最佳答案

ECMAScript 语言规范

15.9.4.2 Date.parse (string)

The parse function applies the ToString operator to its argument and interprets the resulting String as a date and time; it returns a Number, the UTC time value corresponding to the date and time. The String may be interpreted as a local time, a UTC time, or a time in some other time zone, depending on the contents of the String. The function first attempts to parse the format of the String according to the rules called out in Date Time String Format (15.9.1.15). If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats. Unrecognizable Strings or dates containing illegal element values in the format String shall cause Date.parse to return NaN.

您可能做错的是这两个测试不一样:

alert(Date.parse('Mar 1 1990')); 返回一个数字(始终)

alert(new Date('Mar 1 1990')); 返回您提到的字符串

(因为在日期对象上调用了 toString 方法)

关于javascript - Date.parse 的不同行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3824894/

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