javascript - Chrome 和 Firefox 之间的时间戳转换差异

标签 javascript google-chrome date firefox integer

我目前在处理时间戳时遇到了麻烦。我正在 Chrome 24.0.1312.56 m 和 Firefox 18.0.1 控制台中调试以下完全相同的代码:

new Date(parseInt('2012'), parseInt('09') - 1, parseInt('30')).getTime()/1000

当我在 Chrome 中执行它时,我得到:

1348956000

当我在 Firefox 中执行它时,我得到:

1325199600

问题:这里的问题是什么?

最佳答案

对于 parseInt('09'):

  • Chrome 24 似乎返回 9
  • FireFox 18 似乎将该数字视为 octal因此返回 0(0 被解析但 9 没有)

引自 parseInt文档:

Although discouraged by ECMAScript 3, many implementations interpret a numeric string beginning with a leading 0 as octal.
[...]
The ECMAScript 5 specification of the function parseInt no longer allows implementations to treat Strings beginning with a 0 character as octal values.
[...]
Since many implementations have not adopted this behavior as of 2011, and because older browsers must be supported, always specify a radix.

解决方案:修改您的代码并显式指定基数参数:

new Date(parseInt('2012', 10), parseInt('09', 10) - 1, parseInt('30', 10)).getTime()/1000
// 1348945200

关于javascript - Chrome 和 Firefox 之间的时间戳转换差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14542377/

相关文章:

Javascript 帮助,编写 html 元素

javascript - $.getJSON 函数在其他所有操作之后执行

javascript - onclick 事件不适用于位置 :fixed

android - 看不到我的 chrome 设备 ://inspect/#devices

javascript - 使用 PageY 和 ClientY 鼠标位置的差异结果

php - 用于日程安排和考勤卡系统的最佳 php/mysql 日期格式?

java - 生成特定时间之前的日期?

javascript - 显示 Javascript 函数的结果

html - WCAG 文本间距插件

MySQL:将日期列转换为年份列