Component Options: Year, Month, Day
What you'll learn: How to individually control the display of year, month, and day fields when formatting dates with Intl.DateTimeFormat.
Instead of relying on preset dateStyle options, you can gain granular control by specifying exactly which date components to display and how. The year, month, and day options let you compose custom date formats piece-by-piece.
How It Works
Each component accepts specific format values:
year:"numeric"(e.g., 2024) or"2-digit"(e.g., 24)month:"numeric"(3),"2-digit"(03),"long"(March),"short"(Mar), or"narrow"(M)day:"numeric"(7) or"2-digit"(07)
You pass these as options when creating your formatter:
const date = new Date('2024-03-07');
// Year and month only, custom format
const fmt1 = new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: 'long'
});
console.log(fmt1.format(date)); // "March 2024"
// All components with specific formats
const fmt2 = new Intl.DateTimeFormat('en-US', {
year: '2-digit',
month: 'short',
day: '2-digit'
});
console.log(fmt2.format(date)); // "Mar 07, 24"
// Numeric-only approach
const fmt3 = new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit'
});
console.log(fmt3.format(date)); // "03/07/2024"
Think of it like ordering à la carte: Instead of a preset meal (dateStyle: "full"), you pick exactly which ingredients (year, month, day) and how they're prepared (numeric, long, 2-digit).
This is perfect when you need formats like "March 2024" for headers, "07/03" for compact displays, or "Mar 7" for mobile-friendly labels.
Key Takeaway: Use the year, month, and day options with values like numeric, 2-digit, long, or short to build precise, custom date formats without relying on preset styles.