Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Date() constructor doesn't work

Tags:

javascript

I have an issue -

The javascript Date("mm-dd-yyyy") constructor doesn't work for FF. It works fine for IE.

  • IE : new Date("04-02-2008") => "Wed Apr 2 00:00:00 EDT 2008"
  • FF2 : new Date("04-02-2008") => Invalid Date

So lets try another constructor. Trying this constructor Date("yyyy", "mm", "dd")

  • IE : new Date("2008", "04", "02"); => "Fri May 2 00:00:00 EDT 2008"
  • FF : new Date("2008", "04", "02"); => "Fri May 2 00:00:00 EDT 2008"
  • IE : new Date("2008", "03", "02"); => "Wed Apr 2 00:00:00 EDT 2008"
  • FF : 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.

Has anyone dealt with this?
There must be a better way than subtracting 1 from the months.

like image 681
Brad8118 Avatar asked Oct 02 '08 17:10

Brad8118


People also ask

What does new Date () return?

"The expression new Date() returns the current time in internal format, as an object containing the number of milliseconds elapsed since the start of 1970 in UTC.

What is new Date () in JavaScript?

It is used to work with dates and times. The Date object is created by using new keyword, i.e. new Date(). The Date object can be used date and time in terms of millisecond precision within 100 million days before or after 1/1/1970.

How do you code a Date in JavaScript?

We can create a date using the Date object by calling the new Date() constructor as shown in the below syntax. Syntax: new Date(); new Date(value); new Date(dateString); new Date(year, month, day, hours, minutes, seconds, milliseconds);


4 Answers

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 seperate parameters.

BTW, in Firefox,

new Date("04/02/2008"); 

works fine for me - it will interpret slashes, but not hyphens. I think this proves my point that using a String to construct a Date object is problemsome. Use explicit values for month/day/year instead:

new Date(2008, 3, 2); 
like image 185
matt b Avatar answered Sep 29 '22 20:09

matt b


nice trick indeed, which i just found out the hard way (by thinking thru it). But i used a more natural date string with hyphen :-)

var myDateArray = "2008-03-02".split("-"); var theDate = new Date(myDateArray[0],myDateArray[1]-1,myDateArray[2]);  alert(theDate); 
like image 22
joedotnot Avatar answered Sep 29 '22 22:09

joedotnot


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.

like image 29
Frank Avatar answered Sep 29 '22 20:09

Frank


@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.

like image 45
Constantine M Avatar answered Sep 29 '22 20:09

Constantine M