The code
const d = new Date('2021-08-04T18:33:43.0000000Z')
.toLocaleString('en-US', {hour12: false, timeZone: "Asia/Kolkata"})
console.log(d)
is providing result as "8/5/2021, 24:03:43".
Here '24' hr should be shown as '00' hr. Is this a toLocaleString function issue?
I need to use as above syntax because, date format should be browser language date format and time should be 24 hr format.
For other language its works fine, for example GB
const d = new Date('2021-08-04T18:33:43.0000000Z')
.toLocaleString('en-GB', {hour12: false, timeZone: "Asia/Kolkata"})
console.log(d)
will provide result as "05/08/2021, 00:03:43".
So can someone help me how to get the correct 24 hour format time by using toLocaleString function?
Seems to be a bug in INTL ECMA specifications, which should be fixed in ECMA2021.
It may be a Chromium only issue
In the mean time you can add hourCycle: 'h23' to your parameters to force the 00 since h24 is default for SOME locales when hour12: false is specified
https://github.com/moment/luxon/issues/726
The root cause of this issue comes from this change in Chromium. It introduces support for the hourCycle option in Intl.DateTimeFormat.
The four supported values are "h11", "h12", "h23", or "h24" (MDN) and the difference between h23 and h24 is precisely if midnight should be displayed as 00 (h23) or 24 (h24)
What are the differences between the hourCycle options in Date.prototype.toLocaleTimeString()
You have to use hourCycle: 'h23' option to force 0-23 hours format.
> new Date('2021-08-04T18:33:43.0Z').toLocaleString('en-US', { hourCycle: 'h23'})
"8/5/2021, 00:33:43"