javascript - Javascript 毫秒问题

标签 javascript parsing datetime

正如 w3schools 所说,Date.parse() 返回“日期字符串与 1970 年 1 月 1 日午夜之间的毫秒数。”

这意味着
如果我写 Date.parse("January 1, 1970 00:00:00"),它应该给我答案 0。
如果我写 Date.parse("January 1, 1970 00:00:05"),它应该给我答案 5000.. 但我得到的是 -14395000... 为什么是那?

最佳答案

您没有指定时区,因此 January 1, 1970 00:00:00 是您所在时区的时间偏移量(或者更准确地说是浏览器为您选择的时区)。返回的毫秒数是相对于 UTC 的。

MDN Date.parse:

The Date.parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.

[...] If you do not specify a time zone, the local time zone is assumed. GMT and UTC are considered equivalent. The local time zone is used to interpret arguments in RFC2822 Section 3.3 format that do not contain time zone information. [...]

关于javascript - Javascript 毫秒问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23010569/

相关文章:

python - 使用beautifulsoup和python提取标签信息

c++ - 基于转义字符的字符串解析

c# - 具有特定时区的 Javascript 整数到日期时间

javascript - 添加日期 javascript

javascript - 为什么我的场景没有渲染?

javascript - React组件将视频帧连续绘制到 Canvas : doesn't work on iOS

javascript - 缺少字母函数 - 为什么它返回未定义?

javascript - 数组中最后一项的行为异常

iphone - 解析 NSString 加载时间很长

c# - 你如何获得一天中的当前时间?