Human-readable date formatting with vanilla JavaScript

Duration
1 minute read
Last Updated
Mar 2025
Topics
  • code snippet
  • js

Have one of these?

Sat Mar 01 2025 19:50:03 GMT-0500 (Eastern Standard Time)

<!-- or perhaps one of these? -->
<!-- 2025-03-02T01:23:48.693Z -->

Need one of these?

March 1, 2025

Or some other format? You probably don’t need moment.js or piles of StackOverflow-ing into your apartment, you just need Date.toLocaleDateString(). Here’s some common use cases:

YMMV with the en-US locale tag — if you’re based somewhere else you might be better served by using another locale.

Date Only

March 1, 2025

new Date().toLocaleDateString('en-US', {
  month: 'long',
  day: 'numeric',
  year: 'numeric',
});

This .toLocaleDateString() method exists on any Date object. If we’ve got a string that represents a date, we’ll need to parse a Date object from it first:

const someArbitraryDate = new Date(Date.parse('2025-03-02T01:23:48.693Z'));

someArbitraryDate.toLocaleDateString();

3/1/2025

new Date().toLocaleDateString('en-US');

03/01/25

new Date().toLocaleDateString('en-US', {
  month: '2-digit',
  year: '2-digit',
  day: '2-digit',
});

01/03/25 (blimey lol)

new Date().toLocaleDateString('en-GB', {
  month: '2-digit',
  year: '2-digit',
  day: '2-digit',
});

Saturday, March 1, 2025

new Date().toLocaleDateString('en-US', {
  dateStyle: 'full',
});

Time only

Now we’ll mix it up and use Date().toLocaleTimeString()

8:08:03 PM

new Date().toLocaleTimeString();

20:08:03 PM

new Date().toLocaleTimeString('en-US', {
  hour12: false,
});

8:08 PM

new Date().toLocaleTimeString('en-US', {
  timeStyle: 'short',
});

8:08:03 PM EST

new Date().toLocaleTimeString('en-US', {
  timeStyle: 'long',
});

8:08:03 PM Eastern Standard Time

new Date().toLocaleTimeString('en-US', {
  timeStyle: 'full',
});

8:08:03 PM East Africa Time

new Date().toLocaleTimeString('en-US', {
  timeStyle: 'full',
  timeZone: 'Africa/Nairobi',
});

For a full list of time zone options, you can use the TZ identifier column on the Wikipedia list page for tz database time zones.

8:08:03 PM GMT+3

new Date().toLocaleTimeString('en-US', {
  timeZone: 'Africa/Nairobi',
  timeZoneName: 'short',
});

8 PM

new Date().toLocaleTimeString('en-US', {
  hour: 'numeric',
});

Date & Time

You can use Date.toLocaleString to combine the two:

3/1/2025, 8:08:03 PM

new Date().toLocaleString('en-US');

Mar 1, 2025, 8:08 PM

new Date().toLocaleString('en-US', {
  dateStyle: 'medium',
  timeStyle: 'short',
});