SyntaxStudy
Sign Up
javascript

How to Format Dates in JavaScript

Format dates using Intl.DateTimeFormat and the Date object.

JavaScript has the built-in Intl.DateTimeFormat API for locale-aware date formatting — no library needed.

Basic Formatting

Create a formatter with desired options and call .format(date).

Relative Dates

Use Intl.RelativeTimeFormat for "2 days ago" style output.

ISO String

date.toISOString() returns "2024-01-15T10:30:00.000Z".

Avoid Manual Formatting

Don't manually build date strings — use Intl APIs for proper localization.

Example
const date = new Date('2024-06-15');

// Locale-aware formatting
const formatter = new Intl.DateTimeFormat('en-US', {
  year: 'numeric',
  month: 'long',
  day: 'numeric'
});
console.log(formatter.format(date)); // "June 15, 2024"

// Short format
new Intl.DateTimeFormat('en-GB').format(date); // "15/06/2024"

// Relative time
const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
console.log(rtf.format(-2, 'day')); // "2 days ago"
console.log(rtf.format(1,  'month')); // "next month"