javascript - Date.parse 的不同行为?

标签 javascript datetime

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/

相关文章:

javascript - 如何弄清楚如何通过此 jQuery 脚本正确使用 setInterval

java - 有没有办法可以在Java中打印两个纪元时间之间的所有天数?

javascript - 如何将javascript文件导入qml worker脚本

javascript - 我想让播放栏始终可见,但只有在悬停时才可见

C# 如何将两个或多个整数转换为日期?

javascript - 如何处理在 Mongodb 中存储为字符串的日期?

excel - 尽管有日期值,字符串日期未转换为日期类型

javascript - 使用 D3.js SVG 的二维多边形 bool 运算

javascript - 为什么使用替换来取消转义 html 标签也会删除所有其他 html 标签?

javascript - 我应该从 MooTools 转换为 jQuery 吗?