Javascript Invalid Date Error in Internet Explorer
Asked Answered
R

6

46

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/

Repro answered 26/10, 2012 at 17:22 Comment(6)
I actually get invalid date in Firefox as well (latest version, 16.0.1).Campanula
Just make sure you're using a properly supported format. This question has been asked plenty of times before, #3021008Hula
for IE its dateObj = new Date(year, month, date[, hours[, minutes[, seconds[,ms]]]])Alveta
Don't worry. IE hates everyoneCilla
Or Microsoft could just BE NICE like other browsers! .. Terrible browserPup
csgpro.com/blog/2016/08/… . This post will be helpful for someone :)Hodden
R
64

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)
Rotunda answered 26/10, 2012 at 17:26 Comment(4)
thanks, the alternate constructor works however the first one you listed doesn't seem to in any browserRepro
@DougieBear Answer updated - I had missed a required zero before the dayPacific
new Date("2012-11-02T19:30:00.000Z"); The milliseconds can be ommitted, but if included must be preceded by a dot, not a colon.Soliloquy
I had to split my date string and add a T & Z !!Slacken
K
9

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)
Kodak answered 26/10, 2012 at 17:31 Comment(1)
new Date("November 2, 2012 19:30:00"); - This saved me a lot of pain.Ladd
M
3

I was having the same issue with Internet Explorer. This is how I was formatting the date and time initially,

function formatDateTime(date, formatString = 'MM/DD/YYYY hh:mm A') {
  return moment(new Date(date)).format(formatString);
}

The problem was with new Date(). I just removed it as it was already a UTC date. So it is just,

return moment(date).format(formatString);

This worked for me in all browsers including IE.

Magnetomotive answered 16/3, 2017 at 7:14 Comment(2)
OP does not mention using moment.js library.Mode
Other people besides OP view this page looking for answers, and some of them are using moment.jsVasily
M
3

Use

var newDate = moment("2012, 11, 2 19:30:00:000").toDate();
alert(newDate);

This will work in IE too.

Merino answered 6/12, 2018 at 14:25 Comment(0)
U
1

To work in IE, date should be in proper format. I fixed this same issue by using below format:

var tDate = new Date('2011'+"-"+'01'+"-"+'01'); //Year-Month-day
Ursa answered 30/12, 2015 at 7:1 Comment(1)
When I passed this year-month-day format into my code, it fixed the problem... gotta give you a 1 up. However, I didn't need the new Date, just change the format as a string and the date appeared correctly on the server side via IE 11.Spreadeagle
C
0
var time = new Date("2021-12-01 17:02:12");
if(isNaN(time))
{
    time= new Date(date.replace(/ /g,'T')+'.000Z');
}
Cawley answered 1/12, 2021 at 8:2 Comment(1)
Please add some explanation to your answer such that others can learn from itJabez

© 2022 - 2024 — McMap. All rights reserved.