DateTimeFormat Options: Granular Control
You'll learn how to fine-tune date and time output using specific options like dateStyle, timeStyle, weekday, and era.
In the previous lesson, you saw basic time formatting with Intl.DateTimeFormat. Now it's time to customize exactly which parts of a date or time appear and how they're displayed. Think of it like ordering a custom sandwich—you choose each ingredient individually rather than taking a preset combo.
Quick Styles with dateStyle and timeStyle
These options let you set the overall style in one shot:
const date = new Date('2024-03-15T14:30:00');
const short = new Intl.DateTimeFormat('en-US', {
dateStyle: 'short'
}).format(date);
// "3/15/24"
const full = new Intl.DateTimeFormat('en-US', {
dateStyle: 'full'
}).format(date);
// "Friday, March 15, 2024"
const timeOnly = new Intl.DateTimeFormat('en-US', {
timeStyle: 'medium'
}).format(date);
// "2:30:00 PM"
Valid values: "full", "long", "medium", "short".
Granular Component Control
Want more precision? Specify individual components:
const custom = new Intl.DateTimeFormat('en-US', {
weekday: 'long', // "Friday"
year: 'numeric', // "2024"
month: 'short', // "Mar"
day: '2-digit', // "15"
hour: 'numeric', // "2 PM"
minute: '2-digit' // "30"
}).format(date);
// "Friday, Mar 15, 2024, 2:30 PM"
Other useful options include era (for BC/AD), second, timeZoneName, and more. Each component can typically use "numeric", "2-digit", "long", "short", or "narrow" depending on what makes sense.
Key Takeaway: Use dateStyle/timeStyle for quick presets, or combine granular options like weekday, month, era, and hour to build precisely the date/time format your application needs.