I would swear I'm doing this right, as described on MDN:
(new Date()).toLocaleTimeString(undefined, {
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
})
But this is outputting "3:13:22 AM"
....the hour is not 2-digit. What am I doing wrong? I'm very tired.
The hour:'2-digit'
property seems to be ignored by major Chrome, Firefox, Safari and NodeJS. Instead they apply the option hour12:
value (i.e. 12 or 24-hour notation) instead:
hour12:true
results in "3:00:00 PM" since no one really writes "03:00:00 PM" with the presence ofPM
. Doing so may confuse others into thinking that this is 24-hour notation (i.e. 3 AM) if one reads quickly and misses thePM
at the end.
hour12:false
results in "15:00:00" and "PM" is no longer needed.
So in your example, if you wanted 24-hour notation, just use:
let date = (new Date("17 May 2019 3:13:22 AM")).toLocaleTimeString(undefined, {
hour12: false,
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
})
console.log(date); //03:13:22
If I understand correctly, are you wanting the output in 24 hours?
let date = new Date().toLocaleTimeString(undefined, {
hour12: false
});
console.log(date);
EDIT: It seems that your original question is an issue that's been covered prior but classified as a bug. https://bugs.chromium.org/p/chromium/issues/detail?id=507037
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