Date object snippet

Word count icon

79 words

Reading time icon

1 minutes

Getting the locale date

The toLocaleDateString method on the date object lets you pass in the locale of choice and returns a string formatted date with forward slash separators.

JavaScript icon
let today = new Date().toLocaleDateString('en-gb');
// today => 24/09/2020

Replacing the slash

To replace the forward slash with a dash is as easy as manipulating the string with a replace regex.

JavaScript icon
let today = new Date().toLocaleDateString('en-gb').replace(/\//g, '-');
// today => 24-09-2020

Written representation

To get the written representation of the date in the chosen locale toLocaleDateString accepts a second options param where you can specify

  • weekday
  • year
  • month
  • day
JavaScript icon
const options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
let today = new Date().toLocaleDateString('en-gb', options);
// today => Thursday, 24 September 2020