Friday, 18 March 2022

What are valid Date Time Strings in JavaScript?

When using new Date or Date.parse in JavaScript, I cannot just pass arbitrary date formats. Depending on the format, I get a different date than I wanted to or even Invalid Date instead of a date object. Some date formats work in one browser but not in others. So which date time formats should I use?

Additional questions:

  • Do all browsers support the same formats? How do Mozilla Firefox, Google Chrome, Microsoft Internet Explorer, Microsoft Edge, and Apple Safari handle date time strings? What about Node.js?

  • Does it take the local date format into consideration? E.g. if I live in Switzerland and the date format is 30.07.2018, can I use new Date('30.07.2018')?

  • Does it take the local time zone into consideration?

  • How can I get a date time string from a date object?

  • How can I detect invalid date time strings?

  • How do date libraries like Moment.js handle date strings?

In case you did not notice, I answered my own question (why?).



from What are valid Date Time Strings in JavaScript?

No comments:

Post a Comment