How to format ISO date string in Javascript

Issue #705

Supposed we have date in format ISO8601 and we want to get rid of T and millisecond and timezone Z

1
2
3
4
const date = new Date()
date.toDateString() // "Sat Dec 05 2020"
date.toString() // "Sat Dec 05 2020 06:58:19 GMT+0100 (Central European Standard Time)"
date.toISOString() // "2020-12-05T05:58:19.081Z"

We can use toISOString, then split base on the dot . then remove character T

1
2
3
4
date
.toISOString()
.split('.')[0]
.replace('T', ' ')

Comments