为什么我可以解析无效的日期字符串?
Why can i parse invalid date string?
我使用一个函数来检查输入的值是否是我应用程序中特定用途的有效文本。
有效值是一个字符串,它不是有效的日期或数字,既不是真也不是假。
checkText(str) {
return isNaN(str) && isNaN(Date.parse(str)) && ['true', 'false'].indexOf(str) == -1;
}
它工作正常,但我遇到了这个字符串的问题:"New Item 3"。
Date.parse("New Item 3")
return是一个数字,但是为什么!!?此外,如果您将 3 更改为任何小于 13 的数字,它将 return number!
这里有人能给我解释一下发生了什么吗?
经验教训:Date.parse
不是日期验证器。
连 MDN says:
It is not recommended to use Date.parse
as until ES5, parsing of strings was entirely implementation dependent. There are still many differences in how different hosts parse date strings, therefore date strings should be manually parsed (a library can help if many different formats are to be accommodated).
再往下
The ECMAScript specification states: If the String does not conform to the standard format the function may fall back to any implementation–specific heuristics or implementation–specific parsing algorithm. Unrecognizable strings or dates containing illegal element values in ISO formatted strings shall cause Date.parse() to return NaN.
However, invalid values in date strings not recognized as simplified ISO format as defined by ECMA-262 may or may not result in NaN, depending on the browser and values provided
其实问题出在Date.parse()
method,如果你检查:
Date.parse("New Item 3");
会return:
983401200000
console.log(Date.parse("New Item 3"));
所以这里的事实是 Date.parse()
将根据浏览器规范运行,可能 return 也可能不 Number
。 这取决于浏览器。
你可以从 Date.parse()
MDN reference 中看出:
The ECMAScript specification states: If the String does not conform to the standard format the function may fall back to any implementation–specific heuristics or implementation–specific parsing algorithm. Unrecognizable strings or dates containing illegal element values in ISO formatted strings shall cause Date.parse() to return NaN.
However, invalid values in date strings not recognized as simplified
ISO format as defined by ECMA-262 may or may not result in NaN,
depending on the browser and values provided.
我使用一个函数来检查输入的值是否是我应用程序中特定用途的有效文本。
有效值是一个字符串,它不是有效的日期或数字,既不是真也不是假。
checkText(str) {
return isNaN(str) && isNaN(Date.parse(str)) && ['true', 'false'].indexOf(str) == -1;
}
它工作正常,但我遇到了这个字符串的问题:"New Item 3"。
Date.parse("New Item 3")
return是一个数字,但是为什么!!?此外,如果您将 3 更改为任何小于 13 的数字,它将 return number!
这里有人能给我解释一下发生了什么吗?
经验教训:Date.parse
不是日期验证器。
连 MDN says:
It is not recommended to use
Date.parse
as until ES5, parsing of strings was entirely implementation dependent. There are still many differences in how different hosts parse date strings, therefore date strings should be manually parsed (a library can help if many different formats are to be accommodated).
再往下
The ECMAScript specification states: If the String does not conform to the standard format the function may fall back to any implementation–specific heuristics or implementation–specific parsing algorithm. Unrecognizable strings or dates containing illegal element values in ISO formatted strings shall cause Date.parse() to return NaN.
However, invalid values in date strings not recognized as simplified ISO format as defined by ECMA-262 may or may not result in NaN, depending on the browser and values provided
其实问题出在Date.parse()
method,如果你检查:
Date.parse("New Item 3");
会return:
983401200000
console.log(Date.parse("New Item 3"));
所以这里的事实是 Date.parse()
将根据浏览器规范运行,可能 return 也可能不 Number
。 这取决于浏览器。
你可以从 Date.parse()
MDN reference 中看出:
The ECMAScript specification states: If the String does not conform to the standard format the function may fall back to any implementation–specific heuristics or implementation–specific parsing algorithm. Unrecognizable strings or dates containing illegal element values in ISO formatted strings shall cause Date.parse() to return NaN.
However, invalid values in date strings not recognized as simplified ISO format as defined by ECMA-262 may or may not result in NaN, depending on the browser and values provided.