Converting JSON date string /Date()/ to Date object

28 Jun 2018

Microsoft .NET Web APIs returns JSON dates in standardized format by default, but the older versions of .Net framework may serialize the c# datetime object into a strange string format like /Date(1530144000000+0530)/ or /Date(1530144000000)/. The number within the JSON Date string actually denotes the number on milliseconds that have passed since 01-01-1970 (Unix Epoch time).

This way of representation of JSON date was widely used before proper ISO 8601 format was formalized and endorsed by W3C. The major advantage of Unix Epoch format is that it can be stored efficiently as an Integer datatype, easily compared against other values and retrieved faster. so most languages has the default parser (constructor) that can parse and convert it into a date object.

For e.g., In JavaScript following Date constructor straight away converts the milliseconds since 1970 to Date as follows

var myDate = new Date(1530144000000);

The major problem with this Unix Epoch representation is that, it doesn't provide a way to store or represent the timezone offset for the date.

Microsoft's JSON date format:

To overcome the timezone offset limitation, Microsoft has come up with an hacky way of representing the milliseconds along with the timezone offset like /Date(1530144000000+0530)/.

/Date()/ text around the actual milliseconds is used to differentiate that it is custom Microsoft JSON date format from the actual JSON date representation (i.e., just the milliseconds). The .NET JSON date format part contains two parts - i.e., /Date(Milliseconds + Timezone offset)/.

Converting JSON Date string to Local Date:

If you are interested in just the Date part - Milliseconds and doesn't care about the timezone offset, then you can use following simple script to convert as JavaScript Date;

/**
 * Converts the old ASP.NET JSON date format to milliseconds
 * @param date
 */
 public getDateFromAspNetFormat(date: string): number {
     const re = /-?\d+/;
     const m = re.exec(date);
     return parseInt(m[0], 10);
 }


var myDate = new Date(getDateFromAspNetFormat('/Date(1530144000000+0530)/'));

Since we are just picking up the milliseconds and ignoring the timezone. Above method can result in showing entirely a wrong date and time values based on the user's timezone settings i.e., based on the timezone settings of the System/PC. When you do new Date(milliseconds) it creates the date object with the timezone set based on user's machine. As the result, the date will appear different in different countries.

For example,
A price listed up for a fund on today's date ( 29-06-2018 in UTC), may appear as yesterday(28-06-2018 in UTC-12:00) or it may appear as tomorrow (30-06-2018 in UTC+14). This data misrepresentation is unacceptable in most application, that are expected to serve accurate or precise data.

Converting JSON Date string to UTC Date:

In case, you need a complex date manipulations, Timezone parsing & conversions, precise display etc., then it would be best to depend on a well tested DateTime library like moment.js.

With moment.js, you can simply pass the Microsoft's JSON date string. It will be auto-detected and parsed into date object with correct timezone info. This helps you to display the actual datetime in the preferred timezone (let us consider UTC) - formatted and displayed as UTC date string in the browser by using

var myDate = moment.utc('/Date(1530144000000+0530)/');
console.log(myDate.fomat('DD-MM-YYYY'));

 So moment offers a perfect & simple way to handle the TimeZone offsets and convert it into a proper UTC date format.

Jsfiddle showing Json Date to UTC conversion

Related Posts