Reputation: 4702
The javascript Date("mm-dd-yyyy")
constructor doesn't work for FF. It works fine for IE.
new Date("04-02-2008")
=> "Wed Apr 2 00:00:00 EDT 2008"
new Date("04-02-2008")
=> Invalid Date
So let's try another constructor: Date("yyyy", "mm", "dd")
.
new Date("2008", "04", "02");
=> "Fri May 2 00:00:00 EDT 2008"
new Date("2008", "04", "02");
=> "Fri May 2 00:00:00 EDT 2008"
new Date("2008", "03", "02");
=> "Wed Apr 2 00:00:00 EDT 2008"
new Date("2008", "03", "02");
=> "Wed Apr 2 00:00:00 EDT 2008"
So the Date("yyyy", "mm", "dd")
constructor uses an index of 0
to represent January.
How do I best deal with this other than subtracting 1 from the months?
Upvotes: 29
Views: 63092
Reputation: 139921
It is the definition of the Date object to use values 0-11 for the month
field.
I believe that the constructor using a String is system-dependent (not to mention locale/timezone dependent) so you are probably better off using the constructor where you specify year/month/day as separate parameters.
BTW, in Firefox,
new Date("04/02/2008");
It works fine for me - it will interpret slashes, but not hyphens. This proves my point that using a String to construct a Date object is problematic. Use explicit values for month/day/year instead:
new Date(2008, 3, 2);
Upvotes: 45
Reputation: 5133
nice trick indeed, which I just found out the hard way (by thinking through it). But I used a more natural date string with a hyphen :-)
var myDateArray = "2008-03-02".split("-");
var theDate = new Date(myDateArray[0],myDateArray[1]-1,myDateArray[2]);
alert(theDate);
Upvotes: 17
Reputation: 1547
@Frank: you are right. When you need to validate date,
var theDate = new Date(myDate[0],myDate[1]-1,myDate[2]);
will not work.
What happens is that it keeps on adding the extra parameter. For example:
new Date("2012", "11", "57") // Date {Sat Jan 26 2013 00:00:00 GMT+0530 (IST)}
Date object takes the extra days (57-31=26) and adds it to the date we created.
Or if we try constructing a date object with:
new Date("2012", "11", "57", "57") //Date {Mon Jan 28 2013 09:00:00 GMT+0530 (IST)}
an extra 2 days and 9 hours (57=24+24+9) are added.
Upvotes: 2
Reputation: 31
Using
var theDate = new Date(myDate[0],myDate[1]-1,myDate[2]);
Is fine, but it shows some strange behaviors when month and day values are erroneous.
Try casting a date where both myDate[1]-1
and myDate[2]
have values of 55. Javascript still returns a date, though the input is obviously not correct.
I would have preferred javascript to return an error in such a case.
Upvotes: 3
Reputation: 2636
You're quite right, month is indicated as an index, so January is month number 0 and December is month number 11 ...
-- and there is no work-around as it is stated clearly in the ECMA-script-definition, though simple tricks commonly will work:
var myDate = "2008,03,02".split(",");
var theDate = new Date(myDate[0],myDate[1]-1,myDate[2]);
alert(theDate);
Upvotes: 1
Reputation: 21727
Bold statement.
This might have your interest: JavaScript Pretty Date.
Upvotes: 0