Granular Component Formatting
You'll learn how to control exactly which parts of a date or time appear, and how they're formatted, using individual component options.
The Concept
When you used dateStyle and timeStyle in the previous lesson, you got pre-packaged formatting bundles ("short," "medium," "long," etc.). But what if you want a date that shows only the month and year? Or a time with just hours and minutes, no seconds?
Granular component formatting lets you specify exactly which date/time pieces to include and how to display them. Instead of accepting a bundled style, you pass individual options for year, month, day, hour, minute, second, and more.
Think of it like ordering à la carte at a restaurant instead of choosing a fixed combo meal—you pick only the ingredients you want, customized to taste.
How It Works
You pass specific component names as options to Intl.DateTimeFormat or toLocaleString:
const date = new Date('2024-03-15T14:30:00');
// Only month and year
console.log(date.toLocaleString('en-US', {
month: 'long',
year: 'numeric'
}));
// "March 2024"
// Hour and minute only, 12-hour format
console.log(date.toLocaleString('en-US', {
hour: 'numeric',
minute: '2-digit',
hour12: true
}));
// "2:30 PM"
// Full weekday, numeric day
console.log(date.toLocaleString('en-US', {
weekday: 'long',
day: 'numeric'
}));
// "Friday 15"
Each component accepts values like 'numeric', '2-digit', 'short', 'long', or 'narrow' depending on what the component supports.
Why This Matters
Granular control means you can create exactly the date/time display your design requires—no compromises, no workarounds with string manipulation.
Key Takeaway: Granular component formatting gives you precise, per-field control over date and time display by specifying individual options for year, month, day, hour, minute, second, and other components.