Recently, I was working in JavaScript and I need to transform a date into the format YYYYMMDD
, i.e.
- four digit year
- two digit month
- two digit day
For GMT
var date = new Date();
date.toISOString().split('T')[0].replace('-','').replace('-','');
For Local Time
var date = new Date();
return date.getFullYear().toString() +
(1 === (date.getMonth()+1).toString().length ? '0' : '') +
(date.getMonth()+1).toString() +
(1 === date.getDate().toString().length ? '0' : '') +
date.getDate().toString();
For Additional Time Formatting Options
If I need further formatting, I would use a third-party library like Luxon.
Leave a Reply