我目前在处理时间戳时遇到了麻烦。我正在 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/