Parse/Convert JSON Date format

Stimulsoft Reports.JS discussion
rickberon
Posts: 1
Joined: Mon Oct 19, 2020 5:48 am

Re: Parse/Convert JSON Date format

Post by rickberon »

The JSON specification does not specify a format for exchanging dates which is why there are so many different ways to do it. The problem with dates in JSON and JavaScript in general – is that there's no equivalent literal representation for dates. In JavaScript/jQuery following Date constructor straight away converts the milliseconds since 1970 to Date as follows:

Code: Select all

var jsonDate = new Date(1297246301973);
Then let's convert it to js format:

Code: Select all

var date = new Date(parseInt(jsonDate.substr(6)));
The substr() function takes out the /Date( part, and the parseInt() function gets the integer and ignores the )/ at the end. The resulting number is passed into the Date constructor .

For ISO-8601 formatted JSON dates, just pass the string into the Date constructor:

Code: Select all

var date = new Date(jsonDate);
Lech Kulikowski
Posts: 6271
Joined: Tue Mar 20, 2018 5:34 am

Re: Parse/Convert JSON Date format

Post by Lech Kulikowski »

Hello,

Thank you for the information.
Post Reply