0

I am not able to create a Javascript Date object (in Firefox) with 12-hour format time as a parameter to the Date constructor. I tried the following way:

new Date('2021-05-18 04:00 PM+05:30')

This works perfectly in Chrome but not in Firefox and Safari.

Can someone please help in creating a Date object without converting it to a 24-hour time format and which would work on all browsers?

backslashN
  • 2,795
  • 3
  • 15
  • 25
  • I would first try to dig further behind, where do you get this date format from, is it possible that you can get a Date object or a 24h ISO string before it was converted to a 12h format by some library somewhere? btw, I think everyone should [use 24h format](https://www.youtube.com/watch?v=i7L71i9uv3o). – Endless May 26 '21 at 12:17
  • The timestamp format is not one of the two supported by ECMA-262 so parsing is implementation dependent. You should parse and format the string manually, a library can help. – RobG May 26 '21 at 22:22

1 Answers1

0

Well, it doesn't seem to be the correct format to create a date object. Since Date uses Date.parse internally and implementation can be different in both the browsers. So I think the final resort will be to move back to 24-hour format only or set the hours and values separately. See the note here

Note: Parsing of strings with Date.parse is strongly discouraged due to browser differences and inconsistencies.

.

Firefox does not recognise the string you are sending Since it's not according to the formats it supports.

The formats supported are here

Vijay Rathore
  • 593
  • 8
  • 16