Reputation: 277
I've got an oDate source based upon WCF data services. When I browse to it I see dates like:
<d:SignedUp m:type="Edm.DateTime">2001-01-01T00:00:00</d:SignedUp>
When I retrieve this data using jQuery and JSONP and alert out the date, I see:
/Date(978307200000)/
I need to convert this value back to a Date object that I can then format as desired but I can't work out how to do this.
Upvotes: 2
Views: 5747
Reputation: 353
Use something like the following function to convert your Json date to a data object:
function parseJsonDate(jsonDate) {
var offset = new Date().getTimezoneOffset();
var parts = /\/Date\((-?\d+)([+-]\d{2})?(\d{2})?.*/.exec(jsonDate);
if (parts[2] == undefined)
parts[2] = 0;
if (parts[3] == undefined)
parts[3] = 0;
return new Date(+parts[1] + offset + parts[2] * 3600000 + parts[3] * 60000);
};
Upvotes: 1
Reputation: 13320
You can also update your service to OData V3 (WCF Data Services 5.0) and from the client request the JSON to be V3 (MinDataServiceVersion header set to 3.0). In the V3 Verbose JSON the date time format has changed from the /Date(...)/ to the typical XSD format which most jscript libraries should be able to read just fine.
Upvotes: 1