Back to Home
Dates

How to Format Dates

Use toLocaleDateString() or Intl.DateTimeFormat()

Quick Answer (2024 ES6+ Way)

javascript
const date = new Date();
const formatted = date.toLocaleDateString('en-US');
console.log(formatted); // "12/31/2024"

Live Example

javascript
const now = new Date();

// Format for US locale
const usDate = now.toLocaleDateString('en-US');
// "12/31/2024"

// Format with time
const usDateTime = now.toLocaleString('en-US');
// "12/31/2024, 3:45:30 PM"

// Custom format
const custom = new Intl.DateTimeFormat('en-US', {
  year: 'numeric',
  month: 'long',
  day: 'numeric'
}).format(now);
// "December 31, 2024"

Common Variations

ISO Format
javascript
const iso = new Date().toISOString();
// "2024-12-31T15:45:30.000Z"
Relative Time
javascript
const formatter = new Intl.RelativeTimeFormat('en');
formatter.format(-2, 'day'); // "2 days ago"

❌ Don't Do This (Outdated Way)

Avoid toString() or manual string concatenation

javascript
// DON'T DO THIS
var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear();
var formatted = month + '/' + day + '/' + year;

Browser Support

Works in all modern browsers (ES5+)

#dates#formatting#intl