How to handle json DateTime returned from WCF Data Services (OData)

According to this msdn link, DateTime objects are...

...represented in JSON as "/Date(number of ticks)/". The number of ticks is a positive or negative long value that indicates the number of ticks (milliseconds) that have elapsed since midnight 01 January, 1970 UTC.

So you are correct that .NET assumes, but it's UTC instead of GMT (though they are similar). There are some good answers here on SO that give more details and also provide methods for parsing the JSON into a usable date on the client.

As far as converting dates from UTC to a specific time zone, on the server you could use the TimeZoneInfo class which has a ConvertTimeFromUtc method. Or you could write a custom converter that inherits from the JavaScriptConverter class. In javascript, there are the UTC and getTimezoneOffset methods that could be used.

Hope this helps and good luck.


If this may help, I was facing the same problem and I ended to implement something like this, not so elegant but it works.

String.prototype.DateWCF = function(dateformat) {
    return new Date(parseInt(this.match(/\/Date\(([0-9]+)(?:.*)\)\//)[1])).format(dateformat);
};

then on $.ajax success:

        success: function(data) {
            $.each(data, function() {
                var hello = this.DateTimeProperty.DateWCF('dd-MM-yyyy'));
            });
        }

I hope this may be helpful.


This should work just fine:

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.

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

var date = new Date(jsonDate); //no ugly parsing needed; full timezone support

This was already fixed and discussed that a look at this previous post