Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Invalid Date Error in Internet Explorer

Relatively simple javascript here, not sure why IE hates me (treat others how you want to be treated I suppose).

var newDate = new Date("2012, 11, 2 19:30:00:000"); alert(newDate); 

This works in Chrome and FF, but IE outputs "Invalid Date"

Fiddle me this: http://jsfiddle.net/k6yD6/

like image 884
dougmacklin Avatar asked Oct 26 '12 17:10

dougmacklin


People also ask

Why does JavaScript show invalid date?

The JavaScript exception "invalid date" occurs when a string leading to an invalid date has been provided to Date or Date. parse() .

How do I format a date in JavaScript?

The JavaScript toDateString() method returns the date portion of a date object in the form of a string using the following format: First three letters of the week day name. First three letters of the month name. Two digit day of the month, padded on the left a zero if necessary.


2 Answers

The string given to the date constructor should be an RFC2822 or ISO 8601 formatted date. In your example it isn't. Try the following:

new Date("2012-11-02T19:30:00.000Z"); 

or using an alternate constructor:

new Date(2012, 11, 2, 19, 30, 0) 
like image 181
Rich O'Kelly Avatar answered Oct 05 '22 23:10

Rich O'Kelly


IE does not seem to support millisecond and months in Numerical String. Try this:

new Date("November 2, 2012 19:30:00"); 

or

new Date(year, month, day, hours, minutes, seconds, milliseconds) 
like image 24
Guilherme Nascimento Avatar answered Oct 06 '22 00:10

Guilherme Nascimento