Andy Crouch - Code, Technology & Obfuscation ...

Date.parse() With Server Side Strings

JavaScript Code In Editor

Photo: Unsplash

JavaScript date and time parsing is harder than it should be. This is especially true when parsing strings returned from server-side calls. The main problem at present is the fact that Date.parse() is implementation dependent. This means that what works in Chrome will fail spectacularly in all other browsers.

The simplest way to ensure that it works as you expect is to return your strings in ISO 8601 compliant format such as:




This will parse a date or a date and a time correctly. All times are treated as UTC. The topic of time zones and offsets is beyond the scope of this simple post. But, if you need to return a time with an offset then you want:

"yyyy-MM-ddThh:mm:ss +0300"

This will give you a Date object with the time set to 3 hours ahead of the specified time.

It’s funny how often date time string parsing confuses even experienced developers. If you need a thorough reference then the best I have found on this subject is here.

If you know of some good references on this subject or have any thoughts then please share them with me via twitter or email.