How to format a JavaScript date?

Snippets Of JavaScript
There are several ways to format dates in JavaScript - we'll show a 3 popular ways here

Here we'll cover three methods for formatting dates. The first two are built in JavaScript methods. The third is one of the most popular libraries for formatting dates and overcomes the limited functionality of the first two methods.

  1. toLocaleDateString()
  2. toLocaleString()
  3. the momentjs library
  4. using the Luxon library

toLocaleDateString()

The method returns a string with a language-sensitive representation of the date portion of the specified date in the user agent's timezone.

const today  = new Date();
today.toLocaleDateString("en-US") // 9/15/2022

toLocaleDateString() with options

This example uses the options argument to format the date.

  • day: 'numeric' or '2-digit'
  • weekday: 'narrow', 'short', or 'long'
  • year: 'numeric' or '2-digit'
  • month: 'numeric', '2-digit', 'narrow', 'short', or 'long'
  • hour: 'numeric' or '2-digit'
  • minute: 'numeric' or '2-digit'
  • second: 'numeric' or '2-digit'
const options = { 
  weekday: 'long',
  year: 'numeric', 
  month: 'long', 
  day: 'numeric' };
const today  = new Date();
today.toLocaleDateString("en-US", options); // Thursday, September 15, 2022

toLocaleDateString() - Japanese

Another example returning a formatted date in Japanese.

const options = { 
  weekday: 'long',
  year: 'numeric', 
  month: 'long', 
  day: 'numeric' };
const today  = new Date();
today.toLocaleDateString("ja-JP", options);  // 2022年9月15日木曜日

toLocaleString()

The toLocaleString() method returns a string with a language-sensitive representation of this date. It is different to toLocaleDateString in that it returns the time as well.

const today  = new Date();
today.toLocaleString("en-US", { timeZone: 'UTC' }) // 9/15/2022, 4:11:26 PM

momentjs

Use the format() method from moment to a date to a formatted date. Format gives you a lot of flexibility in formatting dates. For example to get a date in teh format January 1, 2022 we'll use the format string "MMMM d, YYYY".

  • DD: Day of the month, zero-padded: 01
  • D: Day of the month: 16
  • Do: Day of the month with: 16th
  • MMMM: Full length month: September
  • MMM: Three character month: Sep
  • MM: Month of the year, with zeroes: 06
  • M: Month of the year: 6
  • YYYY: 4 digit year: 2022
  • YY: 2 digit year: 22

The moment library has been used in millions of projects over the years and is essentially the same design as when it was created in 2011. It uses code from an earlier JavaScript ecosystem. Modern web browsers (and Node.js) expose internationalization and time zone support via the Intl object, codified as ECMA-402. Libraries like Luxon (see the next example) take advantage of this, thus reducing, or removing, the need for additional timezone data files. We recommend using Luxon over Moment in new projects.


const date = new Date();

moment(date).format('MMMM d, YYYY'); // January 1, 2022

Luxon

Luxon is a library for dealing with dates and times in JavaScript". It's a powerful, modern, and friendly wrapper for JavaScript dates and times. It provides DateTimes, durations, and intervals. It's Immutable, chainable, unambiguous API. Has native time zone and Intl support (no locale or tz files needed). Here's an example of of formatting a date with Luxon. (read more...)


const DateTime = luxon.DateTime;
dt = DateTime.now();
dt.setLocale('fr').toLocaleString(DateTime.DATETIME_FULL); // 20 avril 2017 à 11:32 UTC−4