Reputation: 75
I have the date as:
var date = new Date('2021-08-25T00:00:00.000+00:00');
I am formatting time as:
time = date.toLocaleTimeString('en-Us', {
hour12: false,
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
timeZone: 'UTC',
});
Why the time is getting changed to 24:00:00 instead of 00:00:00. And how can i have it as 00:00:00
Upvotes: 7
Views: 1571
Reputation: 4295
To solve it, without changing the locale, use this options: hourCycle: "h23"
and also make sure you remove the: hour12: false
option. For some reason, setting both of them wrongly makes the time "24:00:00"
:
const date = new Date("2021-08-25T00:00:00.000+00:00")
const time = date.toLocaleTimeString("en-US", {
// hour12: false, // Remove this! Otherwise it won't work.
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
timeZone: "UTC",
hourCycle: "h23", // This will show: "00:00:00" instead of "24:00:00".
})
// The time variable equals to: '00:00:00'
Upvotes: 0
Reputation: 20486
Using en-Gb
fixes it.
var date = new Date('2021-08-25T00:00:00.000+00:00');
let time = date.toLocaleTimeString('en-Gb', {
hour12: false,
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
timeZone: 'UTC',
});
console.log(time);
Upvotes: 7