Display 12-Hour and 24-Hour Time in JavaScript
To force a clock style, pass hour12: true|false (or the finer hourCycle) to Intl.DateTimeFormat rather than relying on the locale default. Part of Mastering Intl.DateTimeFormat Options.
Why this scenario is tricky
Whether a locale shows 12- or 24-hour time by default varies, so hard-coding an assumption breaks for someone. The subtlety is midnight: hourCycle distinguishes h11/h12 (12-hour, midnight as 0 or 12) and h23/h24 (24-hour, midnight as 0 or 24), and picking the wrong one prints '24:00' or '0 AM'.
Minimal working solution
const t = new Date('2024-03-15T00:30:00Z');
new Intl.DateTimeFormat('en-US', { hour: 'numeric', minute: '2-digit', hour12: false, timeZone: 'UTC' }).format(t); // '00:30'
new Intl.DateTimeFormat('en-US', { hour: 'numeric', minute: '2-digit', hour12: true, timeZone: 'UTC' }).format(t); // '12:30 AM'
Full production version
// hourCycle is finer than hour12: choose the midnight convention.
new Intl.DateTimeFormat('en-GB', { hour: '2-digit', minute: '2-digit', hourCycle: 'h23', timeZone: 'UTC' });
// 'h23' → 00:00; 'h24' → 24:00; 'h11'/'h12' are 12-hour variants
Verification snippet
Common pitfalls
Frequently Asked Questions
How do I force 24-hour time in JavaScript?
Pass hour12: false to Intl.DateTimeFormat, or use hourCycle: 'h23' for the finer control where midnight is 00:00. For 12-hour output use hour12: true or hourCycle: 'h12'.
What is the difference between hour12 and hourCycle?
hour12 is a simple boolean for 12- vs 24-hour. hourCycle is more precise: h11 and h12 are 12-hour variants (midnight as 0 or 12) and h23 and h24 are 24-hour variants (midnight as 0 or 24), which matters for how midnight and noon are printed.