Time Formatting with DateTimeFormat
What you'll learn: How to display times in 12- or 24-hour formats with seconds and locale-appropriate AM/PM markers using Intl.DateTimeFormat.
Why Time Formatting Matters
You've learned how to format dates with DateTimeFormat, but time display has its own challenges: some regions use 24-hour clocks ("military time"), others prefer 12-hour clocks with AM/PM markers. Different cultures also have varying conventions for displaying seconds and milliseconds.
Controlling Time Components
The DateTimeFormat options object includes specific properties for time formatting:
hour– displays the hourminute– displays minutessecond– displays secondshour12– boolean that forces 12-hour (true) or 24-hour (false) format
const date = new Date('2024-03-15T14:35:22');
// Basic time formatting
const timeFormatter = new Intl.DateTimeFormat('en-US', {
hour: 'numeric',
minute: 'numeric'
});
console.log(timeFormatter.format(date)); // "2:35 PM"
// 24-hour format
const time24 = new Intl.DateTimeFormat('en-US', {
hour: '2-digit',
minute: '2-digit',
hour12: false
});
console.log(time24.format(date)); // "14:35"
// Full time with seconds
const fullTime = new Intl.DateTimeFormat('en-US', {
hour: 'numeric',
minute: '2-digit',
second: '2-digit'
});
console.log(fullTime.format(date)); // "2:35:22 PM"
The locale automatically determines AM/PM markers and default clock preferences, but you can override with hour12:
const japaneseTime = new Intl.DateTimeFormat('ja-JP', {
hour: 'numeric',
minute: 'numeric'
});
console.log(japaneseTime.format(date)); // "14:35" (24-hour default)
Key Takeaway: Use hour, minute, second options to control time display, and hour12 to explicitly choose between 12- and 24-hour formats regardless of locale defaults.