In my timezone, at the moment it's (12:40 AM /00:40).
Date() gives me the hour over 24 as my output looks like 7/8/2022, 24:40:00, how can I fix that?
const dateTimezone = new Date("2022-07-08T00:40:00").toLocaleString('en-US', {
hour12: false
})
console.log(dateTimezone)
The ECMA spec defines the hour12 option to toggle the hour cycle between either the values h11 and h23 or h12 and h24.
This is enforced by Chromium's V8, but not SpiderMonkey and JavascriptCore. Hence the output from the snippet in the question will not be reproducible in most major non-Chromium browsers.
See the open issue on ECMA402 on this topic.
Since the default hour cycle for en-US is h12, if running on V8, hour12: false, will toggle it to h24.
To produce the output you desire consistently across runtimes, you can manually set the hour cycle option to use h23 in .toLocaleString.
The example below should always show 00 for the hour across all runtimes:
const dateTimezone = new Date("2022-07-08T00:40:00").toLocaleString('en-US', {
hourCycle: 'h23'
})
console.log(dateTimezone)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With