Component Options: Hour, Minute, Second
You'll learn how to control time component visibility and precision in formatted dates using Intl.DateTimeFormat options.
Controlling Time Components
Just as you can specify which date parts (year, month, day) to display, Intl.DateTimeFormat lets you control which time components appear and how they're formatted. The hour, minute, and second options determine whether each component shows up and in what format.
Core Time Options
Each time component accepts similar values:
hour:"numeric"(e.g., "1", "13") or"2-digit"(e.g., "01", "13")minute:"numeric"or"2-digit"second:"numeric"or"2-digit"
const date = new Date('2024-03-15T14:05:09');
// Show hours only
const formatter1 = new Intl.DateTimeFormat('en-US', { hour: 'numeric' });
console.log(formatter1.format(date)); // "2 PM"
// Hours and minutes
const formatter2 = new Intl.DateTimeFormat('en-US', {
hour: '2-digit',
minute: '2-digit'
});
console.log(formatter2.format(date)); // "02:05 PM"
// Full time precision
const formatter3 = new Intl.DateTimeFormat('en-US', {
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
console.log(formatter3.format(date)); // "02:05:09 PM"
Practical Application
This granular control matters when displaying timestamps. A clock might need hours and minutes but not seconds. A logger might need all three. A timezone converter might show only hours. You choose what precision your users need.
Key Takeaway: Use hour, minute, and second options with "numeric" or "2-digit" values to control which time components appear and their padding, tailoring time displays to your application's precision requirements.