Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to handle json DateTime returned from WCF Data Services (OData)
    text
    copied!<p>I believe I am missing something obvious here. When I request a JSON response from an OData service I get a different result for the DateTime properties than I do when I request XML. I'll use the NerdDinner OData feed as an example.</p> <p><strong>JSON:</strong></p> <pre><code>http://www.nerddinner.com/Services/OData.svc/Dinners(1)?$format=json "EventDate": "\/Date(1235764800000)\/" </code></pre> <p><strong>XML:</strong></p> <pre><code>http://www.nerddinner.com/Services/OData.svc/Dinners(1) &lt;d:EventDate m:type="Edm.DateTime"&gt;2009-02-27T20:00:00&lt;/d:EventDate&gt; </code></pre> <p>When I do an alert(new Date(1235764800000)) I get this result: <img src="https://i.stack.imgur.com/FVubX.png" alt="alt text"></p> <p>I also get a result of 8PM when I run the same query with LINQPad. <strong>Why is the time zone incorrect in the JSON result?</strong> It seems to assume that the response is in GMT. Should I handle this on the client (via javascript) or is this something that I can set on the server? </p> <p>I'm using jQuery on the client and WCF Data Services (and Entity Framework) on the server.</p> <p><strong>Update:</strong></p> <p>I am using <a href="http://code.google.com/p/datejs" rel="noreferrer">Datejs</a> on the client side to handle the UTC datetime formatting. I'm wondering if this is the correct way to go about this problem. </p> <pre><code> function getDateString(jsonDate) { if (jsonDate == undefined) { return ""; } var utcTime = parseInt(jsonDate.substr(6)); var date = new Date(utcTime); var minutesOffset = date.getTimezoneOffset(); return date.addMinutes(minutesOffset).toString("M/d/yyyy h:mm tt"); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload