0

I have a timestamp like this:

2021-07-23T23:35:00

When I try to format it as follows

var myDate = new Date("2021-07-23T23:35:00").toLocaleTimeString("en-US", { timeStyle: "short" }).toLowerCase();

Chrome will give me (correctly):

11:35 pm

IE11 gives me:

11‎:‎35‎:‎00‎ ‎pm

Safari gives me:

07:35 pm

I've boiled it down to timeStyle: "short", but even after adjusting to:

hour: '2-digit', minute: '2-digit'

Safari still gives me:

07:35 pm

Is there a way to get this with other toLocaleTimeString options?

Thanks!

Best Dev Tutorials
  • 452
  • 2
  • 6
  • 22
  • This is a known issue, https://stackoverflow.com/q/17959660/14032355. Try to pass ```new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)``` in this format instead of passing the string directly. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date. – ikhvjs Jul 13 '21 at 14:48
  • You should really consider using an [ISO](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString) or [UTC](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toUTCString) timestamp to ensure the date is parsed correctly across timezones as well as both client and server side. – Alexander Staroselsky Jul 13 '21 at 15:02

0 Answers0