Chrome中的Date.parse问题 [英] Issue with Date.parse in Chrome
问题描述
例如,Date.parse('foo 2014')应该理想地返回NaN,因为它是不是适当的日期格式。但在Chrome中,这返回值1388514600000,相当于2014年1月1日星期三00:00:00 GMT + 0530(印度标准时间)的日期。
只要字符串以某种年份结束,则返回适当的日期值。这不会让我们正确验证日期。
根据ECMAScript,这种行为是依赖于实现的,chrome会做出非常宽松的验证,一些如何转换为日期。
在Chrome中是否正确验证日期,而不使用任何库或使用重型逻辑和匹配模式?
您可以在解析之前使用正则表达式测试日期字符串。
The implementation of Date.parse in Chrome has very unexpected behavior.
For example, Date.parse('foo 2014') should ideally return NaN as it is not a proper date format. But in Chrome this returns the value 1388514600000, which is equivalent to the date "Wed Jan 01 2014 00:00:00 GMT+0530 (India Standard Time)".
As long as the string ends with some sort of year, a proper date value is returned. This will not let us to properly validate dates.
According to ECMAScript this behavior is implementation dependent and chrome does a very loose validation and some how converts to a date.
Is there anyway in chrome to properly validate date without using any library or using heavy logic and matching patterns?
You can test date string with regular expression before parse.
这篇关于Chrome中的Date.parse问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!