home ~ projects ~ socials

Parse An ISO Date In JavaScript

The native Date JavaScript object can parse ISO style date string. For example, this one without a timezone:

const dateString = '2021-11-12T02:03:03'
const dateObject = new Date(dateString)

That code will produce a date object that will print out this if logged:

2021-11-12T07:03:03.000Z

Note that the time has moved to Zulu (or GMT) time. That is, the hour shifted from 2 to 7.

Once the date is parsed, you use functions like:

.getFullYear()
.getMonth()
.getDate()

And all the other ones listed here

Note, this is done without using Data.parse() which is "strongly discouraged due to browser differences and inconsistencies." (source

-- end of line --