我有下面的代码,它应该尝试解析给定的日期。如果失败,则默认为当前日期:
var date = new Date(textbox.value); // Try to parse
if (isNaN(date)) date = new Date(); // Default to current
现在我使用isNaN()测试它是否能够正确解析,例如检查new Date()是否返回“Invalid Date”。
这在IE、FF和Chrome上似乎可以正常工作,但在safari上不起作用。例如,如果它试图解析日期的空字符串,有时会认为是1970年,有时会认为是无效日期。以下是来自safari JS控制台的练习:
a=new Date("") <-- This thinks it's 1970 for some reason
Thu Jan 01 1970 11:00:00 GMT+1100 (AUS Eastern Daylight Time)
b=new Date("abc") <-- Fails of course, which is good
Invalid Date
c=new Date("") <-- Why does this fail now, whereas it thought it was 1970 before?
Invalid Date
isNaN(a) <-- Why is it successfully parsing an empty string for a date?
false
isNaN(b) <-- Fair enough
true
isNaN(c) <-- This is what i'd expect, but why is it different now?
true
不知道这里发生了什么,非常感谢