gpt4 book ai didi

javascript - ISO 8601 测试用例的 ECMAScript 5 Date.parse 结果

转载 作者:行者123 更新时间:2023-11-29 10:21:53 38 4
gpt4 key购买 nike

以下测试用例的正确结果是什么?

                                                    //Chrome 19     Opera 12       Firefox 11    IE 9          Safari 5.1.1
console.log(Date.parse("2012-11-31T23:59:59.000Z"));//1354406399000 NaN NaN 1354406399000 NaN
console.log(Date.parse("2012-12-31T23:59:59.000Z"));//1356998399000 1356998399000 1356998399000 1356998399000 1356998399000
console.log(Date.parse("2012-12-31T23:59:60.000Z"));//NaN NaN NaN NaN 1356998400000
console.log(Date.parse("2012-04-04T05:02:02.170Z"));//1333515722170 1333515722170 1333515722170 1333515722170 1333515722170
console.log(Date.parse("2012-04-04T24:00:00.000Z"));//NaN 1333584000000 1333584000000 1333584000000 1333584000000
console.log(Date.parse("2012-04-04T24:00:00.500Z"));//NaN NaN 1333584000500 1333584000500 NaN
console.log(Date.parse("2012-12-31T10:08:60.000Z"));//NaN NaN NaN NaN 1356948540000
console.log(Date.parse("2012-13-01T12:00:00.000Z"));//NaN NaN NaN NaN NaN
console.log(Date.parse("2012-12-32T12:00:00.000Z"));//NaN NaN NaN NaN NaN
console.log(Date.parse("2012-12-31T25:00:00.000Z"));//NaN NaN NaN NaN NaN
console.log(Date.parse("2012-12-31T24:01:00.000Z"));//NaN NaN NaN 1356998460000 NaN
console.log(Date.parse("2012-12-31T12:60:00.000Z"));//NaN NaN NaN NaN NaN
console.log(Date.parse("2012-12-31T12:00:60.000Z"));//NaN NaN NaN NaN 1356955260000
console.log(Date.parse("2012-00-31T23:59:59.000Z"));//NaN NaN NaN NaN NaN
console.log(Date.parse("2012-12-00T23:59:59.000Z"));//NaN NaN NaN NaN NaN
console.log(Date.parse("2012-02-29T12:00:00.000Z"));//1330516800000 1330516800000 1330516800000 1330516800000 1330516800000
console.log(Date.parse("2011-02-29T12:00:00.000Z"));//1298980800000 NaN NaN 1298980800000 NaN
console.log(Date.parse("2011-03-01T12:00:00.000Z"));//1298980800000 1298980800000 1298980800000 1298980800000 1298980800000

// extended years:
console.log(Date.parse("0000-01-01T00:00:00.000Z"));//-621672192e5 -621672192e5 -621672192e5 -621672192e5 -621672192e5
console.log(Date.parse("+275760-09-13T00:00:00.000Z"));//8.64e15 NaN 8.64e15 8.64e15 8.64e15
console.log(Date.parse("-271821-04-20T00:00:00.000Z"));//-8.64e15 NaN -8.64e15 -8.64e15 -8.6400000864e15
console.log(Date.parse("+275760-09-13T00:00:00.001Z"));//NaN NaN NaN 8.64e15 + 1 8.64e15 + 1
console.log(Date.parse("-271821-04-19T23:59:59.999Z"));//NaN NaN NaN -8.64e15 - 1 -8.6400000864e15 - 1

// https://github.com/kriskowal/es5-shim/issues/80 Safari bug with leap day
console.log(Date.parse("2034-03-01T00:00:00.000Z") -
Date.parse("2034-02-27T23:59:59.999Z"));//86400001 86400001 86400001 86400001 1

// Time Zone Offset
console.log(Date.parse("2012-01-29T12:00:00.000+01:00"));//132783480e4 132783480e4 132783480e4 132783480e4 132783480e4
console.log(Date.parse("2012-01-29T12:00:00.000-00:00"));//132783840e4 132783840e4 132783840e4 132783840e4 132783840e4
console.log(Date.parse("2012-01-29T12:00:00.000+00:00"));//132783840e4 132783840e4 132783840e4 132783840e4 132783840e4
console.log(Date.parse("2012-01-29T12:00:00.000+23:59"));//132775206e4 132775206e4 132775206e4 132775206e4 132775206e4
console.log(Date.parse("2012-01-29T12:00:00.000-23:59"));//132792474e4 132792474e4 132792474e4 132792474e4 132792474e4
console.log(Date.parse("2012-01-29T12:00:00.000+24:00"));//NaN 1327752e6 NaN 1327752000000 1327752000000
console.log(Date.parse("2012-01-29T12:00:00.000+24:01"));//NaN NaN NaN 1327751940000 1327751940000
console.log(Date.parse("2012-01-29T12:00:00.000+24:59"));//NaN NaN NaN 1327748460000 1327748460000
console.log(Date.parse("2012-01-29T12:00:00.000+25:00"));//NaN NaN NaN NaN NaN
console.log(Date.parse("2012-01-29T12:00:00.000+00:60"));//NaN NaN NaN NaN NaN
console.log(Date.parse("-271821-04-20T00:00:00.000+00:01"));//NaN NaN NaN -864000000006e4 -864000008646e4
console.log(Date.parse("-271821-04-20T00:01:00.000+00:01"));//-8.64e15 NaN -8.64e15 -8.64e15 -864000008640e4

似乎 ECMAScript 应该更具体地针对边缘情况;目前是说无效的日期格式取决于实现,对吗?

最佳答案

根据 ES5 规范,Date.parse仅适用于有效的 ISO 8601日期。其他任何内容都取决于实现(实际上,IE < 9 不适用于标准 ISO 日期,它需要一个“/”分隔符)。所以如果你给它一个无效的日期(比如 2012-11-31)你会得到任何东西,从 2012-12-01 到一个错误。

在你的测试中:

2012-12-31T23:59:60.000Z

应该可以工作,但可能不像您预期​​的那样。使用 60 秒表示闰秒,它不等于 24:00:00,只有 Safari 似乎正确。

还有:

2012-04-04T24:00:00.000Z

应该可以,它表示 2012 年 4 月 4 日午夜,因此 Firefox 在那里出错。

ES5 实现应支持的格式在 spec 中.

哦,您可能还应该测试省略“T”(因为在某些情况下它是可选的,我认为包括浏览器)和不同的时区,例如:

2012-04-03 23:50:00+10:00
2012-04-03 23:50:00-04:15
2012-04-03 23:50:00+10
20120403T235000+1000

等等 YYYYDDD 和 YYYYWwwD 格式,尽管实现不需要支持它们。

关于javascript - ISO 8601 测试用例的 ECMAScript 5 Date.parse 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10005374/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com