Skip to content
Home » Javascript’s Date() does not work with IE & Safari

Javascript’s Date() does not work with IE & Safari

Date() does not work with IE & Safari

Working with Date() in programming is a little tricky so in this post, we will see when the Date() does not work in IE and Safari. Also will learn how to fix it.


  • Most languages have come up with some built-in functionality to help with date function.
  • JavaScript in particular has lots of useful functions to aid in getting, setting, and outputting dates.
  • This simple basic code below works in all browsers because it simply gives the current date-time.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
new Date();
new Date();
new Date();

Now The Problem

  • When it comes to passing and calculating dates using the new Date(), the below code will not work on IE or Safari.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
new Date("2020-08-26 12:29:33");
// Firefox also returns ‘Invalid Date’ whereas it works in Chrome.
new Date("10-26-2020");
new Date("2020-08-26 12:29:33"); // Firefox also returns ‘Invalid Date’ whereas it works in Chrome. new Date("10-26-2020");
new Date("2020-08-26 12:29:33");

// Firefox also returns ‘Invalid Date’ whereas it works in Chrome.
new Date("10-26-2020");
  • Here, IE returns ‘NaN’ and Safari returns ‘Invalid Date’ because for some reason, IE and Safari do not support this type of date format “YYYY-MM-DD”.

And the Solution

  • So the solution is to pass a valid date format to Date() Object that supports across all browsers.
  • The following are some valid date formats:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// yyyy, mm-1, dd
new Date(2020, 09, 26);
// yyyy, mm-1, dd, hh, mm, ss
new Date(2020, 09, 26, 11, 12, 29);
// "mm/dd/yyyy"
new Date("10/26/2020");
// "mm/dd/yyyy hh:mm:ss"
new Date("10/26/2020 12:29:29");
// milliseconds
new Date(1608921000000);
// "Day Month dd yyyy hh:mm:ss GMT"
new Date("Sat Dec 26 2020 11:12:29 GMT");
// yyyy, mm-1, dd new Date(2020, 09, 26); // yyyy, mm-1, dd, hh, mm, ss new Date(2020, 09, 26, 11, 12, 29); // "mm/dd/yyyy" new Date("10/26/2020"); // "mm/dd/yyyy hh:mm:ss" new Date("10/26/2020 12:29:29"); // milliseconds new Date(1608921000000); // "Day Month dd yyyy hh:mm:ss GMT" new Date("Sat Dec 26 2020 11:12:29 GMT");
// yyyy, mm-1, dd
new Date(2020, 09, 26);

// yyyy, mm-1, dd, hh, mm, ss
new Date(2020, 09, 26, 11, 12, 29);

// "mm/dd/yyyy"
new Date("10/26/2020");

// "mm/dd/yyyy hh:mm:ss"
new Date("10/26/2020 12:29:29"); 

// milliseconds
new Date(1608921000000);

// "Day Month dd yyyy hh:mm:ss GMT"
new Date("Sat Dec 26 2020 11:12:29 GMT");

I hope this helps you understand and fix the Date() format issue with IE and Safari.


References


Related Posts

Visit https://techtalkbook.com to find more related topics.

Happy Coding!!!


Leave a Reply

Your email address will not be published. Required fields are marked *