Javascript’s Date() does not work with IE & Safari — Technolize Your Future

Sandeep Agrawal
2 min readOct 11, 2020

--

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.
new Date();

Now The Problem

new Date("2020-08-26 12:29:33"); new Date("10-26-2020"); 
// Firefox also returns 'Invalid Date' where as it works in Chrome.

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:
// 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

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

Happy Coding!!!

Originally published at https://techtalkbook.com on October 11, 2020.

--

--