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 - Electron - 将文件下载到特定位置

javascript - 在 javascript es6 中编写全局 const 的正确性是什么?

google-chrome - 如何强制 Chrome 重新加载资源?

google-chrome - Chrome说到有关插件Cookie的问题

mysql - SQL 默认值当前日期和时间

javascript - 放大时热图单元格之间的白线(浏览器缩放)

javascript - 如何正确设置浏览器创建的桌面通知的关闭超时

javascript - Webpack 编译的 Chrome 扩展抛出 `unsafe-eval` 错误

PHP - 手动创建包含与格林威治差异的日期

Java - Oracle - DB Insert - Timestamp to Date 列