Course contentsShow
← Web Development
Lesson 2769 of 2,98249. Dates, Times, and Numbers on the WebFree lesson

DateTimeFormat Options and Styles

Controlling date and time output with options like dateStyle, timeStyle, and component-level formatting.

DateTimeFormat Options and Styles

You'll learn how to precisely control date and time output using Intl.DateTimeFormat options like dateStyle, timeStyle, and component-level formatting.

Why Control Formatting?

When displaying dates and times to users, you rarely want the raw default format. Sometimes you need "Wednesday, January 15, 2025" for a formal document, other times just "1/15/25" for a compact UI. The Intl.DateTimeFormat API gives you two approaches: high-level styles for quick consistency, or component-level options for surgical precision.

High-Level Styles: dateStyle and timeStyle

These options let you choose preset formatting levels without specifying individual components:

const date = new Date('2025-01-15T14:30:00');

// Date only, various styles
console.log(new Intl.DateTimeFormat('en-US', { dateStyle: 'full' }).format(date));
// "Wednesday, January 15, 2025"

console.log(new Intl.DateTimeFormat('en-US', { dateStyle: 'short' }).format(date));
// "1/15/25"

// Time only
console.log(new Intl.DateTimeFormat('en-US', { timeStyle: 'medium' }).format(date));
// "2:30:00 PM"

// Both together
console.log(new Intl.DateTimeFormat('en-US', { 
  dateStyle: 'long', 
  timeStyle: 'short' 
}).format(date));
// "January 15, 2025 at 2:30 PM"

Values for both: 'full', 'long', 'medium', 'short'.

Component-Level Precision

When styles aren't enough, specify exactly which parts to show:

console.log(new Intl.DateTimeFormat('en-US', {
  year: 'numeric',
  month: 'long',
  day: 'numeric'
}).format(date));
// "January 15, 2025"

Key Takeaway: Use dateStyle and timeStyle for consistent, locale-appropriate formatting with minimal code; switch to component-level options like year, month, and day when you need precise control over which parts appear and how.