Does anyone know why in Firefox if you execute the code below it will validate it as a date if the string passed in is four numbers and only four numbers? In every other browser I tested with (IE, Chrome) it will always return as not a date.
Being that the spec, as pointed out by Marcel Korpel below, states that it should fall back to use the Firefox's implementation-specific fall back I am really wondering why Firefox's fall back displays this anomaly.
function isDate(sDate) {
var temp = new Date(sDate);
if (temp.toString() == "NaN" || temp.toString() == "Invalid Date") {
alert("Not a Date");
} else {
alert("Is a Date!");
}
}
Does anyone know why in Firefox if you execute the code below it will validate it as a date if the string passed in is four numbers and only four numbers? In every other browser I tested with (IE, Chrome) it will always return as not a date.
Being that the spec, as pointed out by Marcel Korpel below, states that it should fall back to use the Firefox's implementation-specific fall back I am really wondering why Firefox's fall back displays this anomaly.
function isDate(sDate) {
var temp = new Date(sDate);
if (temp.toString() == "NaN" || temp.toString() == "Invalid Date") {
alert("Not a Date");
} else {
alert("Is a Date!");
}
}
Share
Improve this question
edited Sep 2, 2010 at 13:47
Brian S.
asked Sep 2, 2010 at 12:54
Brian S.Brian S.
4053 silver badges11 bronze badges
4
- Please indent your code with 4 spaces, don't use backticks the way you did. – Marcel Korpel Commented Sep 2, 2010 at 12:56
- return true in one branch, and don't return anything in another... ???!? – Jason S Commented Sep 2, 2010 at 12:57
- Yea I was trying to make a quick and dirty isDate function and forgot to add in the return false. Let me just remove the return true for this discussion. – Brian S. Commented Sep 2, 2010 at 13:15
-
Why do you use
toString
to test forNaN
? Why not useisNaN(temp)
? – Marcel Korpel Commented Sep 2, 2010 at 13:22
4 Answers
Reset to default 5If you pass a string to the Date
constructor, the string should be in a format recognized by the parse method (IETF-pliant RFC 1123 timestamps) (source: MDC). Everything else results in implementation specific behaviour and will vary across browsers.
I suggest you don't use strings at all and either use three numbers representing year, month and day (mind that month numbers begin at 0 (= January)), or use one number, the number of milliseconds since 1 January 1970 00:00:00 UTC.
UPDATE: seeing your example,
var a = new Date('0123');
console.log(a);
outputs
Fri Jan 01 0123 01:00:00 GMT+0100 (CET)
so Firefox apparently recognizes '0123'
as a year number.
UPDATE 2: I think MDC's description of Date.parse
contains the answer to your question:
Starting in JavaScript 1.8.5, a subset of ISO 8601 formatted date strings can also be parsed.
The ISO 8601 page specifies (section 'Formats'):
Year:
YYYY (eg 1997)
Year and month:
YYYY-MM (eg 1997-07)
Complete date:
YYYY-MM-DD (eg 1997-07-16)
So when relying on ISO 8601, a string only containing four numbers will be recognized as a year number.
Do not rely on date validation in javascript. The local date time format may create some problem with the javascript date object.
use following approach
- show non editable text box
- on focus of the textbox show calendar
- do empty validation, it the field has to be mandatory
- no need to perform date validations as if the value of text box is not empty, it will be definitely date. We are not allowing user to enter anything else and the selected value from the calendar is always gonna be Date :)
This works in all browsers -
new Date('2001/01/31 12:00:00 AM')
I have encountered the same issue as with this in firefox, for some reasons I cannot explain any 4 digit numeric chars is a valid date in FF, in other browsers this is NaN:
A bit nasty work-around for FF but, this worked for me:
function isDate(sDate) {
if(sDate.match(/^\d{4}$/))
return false;
var temp = new Date(sDate);
if (temp.toString() == "NaN" || temp.toString() == "Invalid Date") {
alert("Not a Date");
} else {
alert("Is a Date!");
return true;
}
}