Andrew
Andrew

Reputation: 339

toLocaleDateString time formatting issue (Firefox vs. Chrome)

I'm seeing different results for this date formatting command in Chrome and Firefox:

new Date(1156550400000).toLocaleDateString('en-us', {weekday: 'long', month: 'short', day: 'numeric', hour: '2-digit', hour12: false, timeZone: 'UTC'})+'z'

in Firefox, I get this result (this is the desired output format):

Saturday, Aug 26, 00z"

in Chrome, I get this result:

"Saturday, Aug 26, 24z"

Easy enough to check for the "24" value and replace with "00", but hoping for a built in method... Is there an option that I'm missing in the toLocaleDateString options for formatting, or an alternate approach to get the desired format?

Upvotes: 1

Views: 521

Answers (1)

ScienceKitten
ScienceKitten

Reputation: 111

I see that option 'hour12' works differently in Mozilla Firefox and Google Chrome. May be, using option 'hourCycle' instead of 'hour12' is helpful in this case.

new Date(1156550400000).toLocaleDateString('en-us', {weekday: 'long', month: 'short', day: 'numeric', hour: '2-digit', hourCycle: "h23", timeZone: 'UTC'})+'z'

In Mozilla Firefox (82.0 (64-bit)) this returns "Saturday, Aug 26, 00z". In Google Chrome (Version 86.0.4240.111 (Official Build) (64-bit)) this returns "Saturday, Aug 26, 00z".

Upvotes: 1

Related Questions