Original Question: How do I get the hour/month to respect a '2-digit' formatting.
const event = new Date(2012, 3, 20, 3, 0, 0);
Edit... Apologies all, I don't use this very often
The real issue is depending on which version of chrome you are on, it respects this formatting differently:
For example:
new Date(1561984526000).toLocaleString("ja-JP", {hour: "2-digit"})
// Chrome 80 (and other releases): "08時"
// Chrome 79: "8時"
The main problem is that you are passing options
in the first parameter, which is for the locale string. options
belongs in the second parameter.
You also need to include the other fields (year, day, minute) if you want them in the results.
const event = new Date(2012, 3, 20, 3, 0, 0);
console.log(event.toLocaleString('en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit' }
));
You can pass undefined
in the first parameter if you want it to use the user's current locale.
const event = new Date(2012, 3, 20, 3, 0, 0);
console.log(event.toLocaleString(undefined, {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit' }
));
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