2

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.

3
  • The result looks like a regexp. Could you please check the type? (Via typeof data, and in case it's object, Object.prototype.toString.call(data).)
    – fb55
    Jun 28, 2012 at 10:53
  • Does jQuery really parse 2001-01-01T00:00:00 into /Date(978307200000)/ by its own? Don't you have some code of your own that actually does so? Jun 28, 2012 at 10:54
  • Also, do you actually use JSONP to read XML or it's just a typo in the question? Jun 28, 2012 at 10:55

2 Answers 2

1

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.

0
1

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);
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.