JavaScript new Date() Returning NaN in IE or Invalid Date in Safari

February 8th, 2011 - Posted by Steve Marks to Javascript / jQuery, Web Development.

When it comes to programming, working with dates can be tricky. Luckily however most languages have done the hard work already and come with some kind of built-in date functionality to assist us. JavaScript inparticular has lots of useful functions to aid in getting, setting and outputting dates.

The JavaScript Date Object

To begin working with dates in JavaScript the first thing you need to do is initialise a date object like so:

var d = new Date();

That’s fine if we want get the current date and time, however it doesn’t help us much if we are planning on working with a date in the past or future. In this instance we would need to pass a date as a parameter to the code above.

The Problem

The problem I wanted to discuss today is specific to Internet Explorer and Safari and is about how passing a date as shown below doesn’t work as expected:

var d = new Date("2011-02-07");


var d = new Date("2011-02-07T11:05:00");

If you were run the above code snippets across various browsers you would see that in IE you get ‘NaN’ returned and in Safari you get ‘Invalid Date’. Firefox, Chrome and Opera however will output the correct date.

The Solution

The problem lies in the format that you pass the required date to the Date() object. For some reason, and don’t ask me why, the two aforementioned browsers surprisingly do not support the date format “yyyy-mm-dd” and therefore fail. I haven’t managed to compile a definitive list of supported date formats, however I can tell you the following formats are definitely supported across all browsers and would advise sticking to one of these to avoid errors:

var d = new Date(2011, 01, 07); // yyyy, mm-1, dd
var d = new Date(2011, 01, 07, 11, 05, 00); // yyyy, mm-1, dd, hh, mm, ss
var d = new Date("02/07/2011"); // "mm/dd/yyyy"
var d = new Date("02/07/2011 11:05:00"); // "mm/dd/yyyy hh:mm:ss"
var d = new Date(1297076700000); // milliseconds
var d = new Date("Mon Feb 07 2011 11:05:00 GMT"); // ""Day Mon dd yyyy hh:mm:ss GMT/UTC
This entry was posted on Tuesday, February 8th, 2011 at 12:33 am by +Steve Marks and is filed under Javascript / jQuery, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.

Fear not, we won't publish this

Comments (34)
  1. Irene says:

    That’s of great help! I was really struggling with this.
    But now I refactored again using the moment.js library where you can parse the date with the ISO_8601 param which works on Safari as well.
    See here:

  2. Joe says:

    After hours of searching the reason why my scheduler runs in problem in some browsers, I found this site => That is fantastic – thanks

  3. H├ęctor says:

    Hi .. Thanks for your help, helped me to solve my problem with dates and IE browser.

    Greetings from Temuco – Chile.

  4. Max says:

    Never mind my last comment, a hard refresh was needed to make Safari on iOS behave the same ;-) Thanks for the article!

  5. Max says:

    Intially I got my date comparing code working everywhere except on Safari (Desktop) and iOS (Safari and Chrome, which is based on Safari). Based on this article I chose to compare timestamps instead of date strings, and I got it working now also on Safari (Desktop) and Chrome on iOS, but still no Safari on iOS (which is strange because on iOS Chrome runs on Safari I believe). Running out of ideas, any suggestions? Thanks.

  6. slogic says:

    Just tested, IE9 and later also supopports “2015-10-07T00:00:00+03:00”-like values.

  7. slogic says:

    IE9 started to understand format “2013-01-01T01:01:01”, but not “2013-01-01 01:01:01” or “2015-10-07T00:00:00+0300”. Prior versions are much dumber.

  8. Nikolay says:

    I faced the same issue and resolved it in a minutes with your help, thank you!

  9. Kerry Mraz says:

    This worked wonders for my issue. Format yy-mm-dd works great in all other browsers I tested. Comes out with NAN-NAN-NAN in IE11. Change format to yy/mm/dd and it works like a charm. Thanks for posting this!

  10. Susmita says:

    This worked for me..Thanks

  11. Abdul Moeed says:

    You Rocks. that’s working for me

  12. Jonas says:

    Thank you for this! Been messing around for a few days with this problem now until I got to your site!

  13. ant says:

    Worked great once I realized that “mm/dd/yyyy” was actually “MM/dd/yyyy” – Thanks ;-)

  14. santhosh says:

    Thanks a lot!

  15. Mariusz says:

    Thanks a lot!

  16. Bryan says:

    Life Saver!

  17. binaya says:

    Thanks for the post.
    Its really solve the big issue for me.

  18. Dennis says:

    thank you!!! it was a big problem

  19. B says:

    Thanks a MILLION!! Very straight-forward and works great. Saved my bacon!

  20. I was having so many issues with this until I narrowed it down to the problem being the Date function in IE so after a quick google I came across your post and I just wanted to say thank you for posting a solution, You Rock!

  21. Leonardo Cardozo says:

    Excelente ayuda amigo!
    Muchas gracias!
    Thanks a lot!!

  22. dhivya says:

    i searched a lot… this is very useful.. thanks a lot:) keep posting

  23. hey thanks man , good explanation, which was hard to find!!
    keep it up!!

  24. Kasi says:

    Thank you so much for the easy explanation. It works.

  25. Alex says:

    Thank you so much! I’ve been racking my brain for two hours straight, trying to figure out how to resolve this problem.

  26. Shraddha says:

    Thankyou so much for this post. Just needed this urgently to fix a big bug. God Bless You :-).

  27. Borisz says:

    Thanks a lot!!!!
    A had debugged five hours, starting with the guess, that the page brokes because of some ajax request problem – but no, that was the real problem. Thanks again!

  28. Ntpnhan says:

    Thanks a lot. This article help me very much.

  29. David says:

    Thanks a lot, I couldn’t understand where the problem came from, you saved me ;)

  30. thanks man……this post did help me.

  31. I just use slashes instead of hyphens.

  32. Vibha says:

    Thanks a lot, you saved my time… Now time works on all browser

  33. Joris says:

    Thanks a lot for the information. I browsed a lot of websites and stackoverflow solutions but none resulting in a working solution. This works like a charm.

  34. Dan says:

    Thanks, saved me some time testing what format each browser supports.

    All I needed was 2011-02-07T11:05:00 and 2011-02-07 to work but no such luck. They seem fine in Chrome and FireFox but not IE.

    Now I’m going to use milliseconds instead, I’m avoiding mm/dd/yyyy as I’m in the UK and it makes no sense to use this.

    I’m surprised however that when I pull dates from MS SQL I can format them as 2011-02-07T11:05:00 but not milliseconds (without doing extra work) yet in MS IE its the other way around 2011-02-07T11:05:00 is not supported yet milliseconds is.

    Just another reason to get p!$$ed off at MS datetime support