Get a short Locale Date string using JavaScript


Borislav Hadzhiev

Last updated: Jan 23, 2022


Photo from Unsplash

Get a short Locale Date string using JavaScript #

Use the toLocaleDateString() method to get a short locale date string. The method returns a string representing the date portion for the Date object according to the provided locale and options arguments.

const date = new Date(); // 👇️ "Sun, 01/23/2022" console.log( date.toLocaleDateString('en-US', { year: 'numeric', month: '2-digit', weekday: 'short', day: 'numeric', }), );

The toLocaleDateString() method returns a string representation of the date according to the provided parameters.

The two parameters we passed to the method are:

  1. locales - a string with a BCP 47 language tag or an array of such strings. You can use any of the available locales, e.g. es-MX for Mexico or en-CA for Canada. If you need more information about this parameter, check out the MDN docs.
  2. options object, where we can adjust the formatting style. Read more about the options object in the MDN docs.

The year property can be set to:

  • numeric - e.g. 2022
  • 2-digit - e.g. 22

The month property can be set to:

  • numeric - e.g. 9
  • 2-digit - e.g. 09
  • long - e.g. September
  • short - e.g. Sep
  • narrow - e.g. S

The weekday property can be set to:

  • long - e.g. Thursday
  • short - e.g. Thu
  • narrow - e.g. T

The day property can be set to:

  • numeric - e.g. 4
  • 2-digit - e.g. 04

Note that when the narrow formatting is used for the month, two months may have the same narrow style for some locales, e.g. July and June are both represented as J.

If you don't need to show the name of the weekday, you can also set the dateStyle property to short.

const date = new Date(); // 👇️ "1/23/22" console.log( date.toLocaleDateString('en-US', { dateStyle: 'short', }), );

This formats the date string as mm/dd/yy for the en-US locale.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.