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.
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.
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
const options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
};
let today = new Date().toLocaleDateString('en-gb', options);
// today => Thursday, 24 September 2020
Top comments (0)