Last post Apr 20, 2016 10:07 PM by stickian
Aug 04, 2014 11:32 AM|Ben Crinion|LINK
My WebAPI OData endpoint is doing something strange when it's serialising dates.
If I don't add a select to the query, for example, http://localhost:50469/efodata/Jobs?&$filter=JobId%20eq%2067, all my dates which have a DateTimeKind of UTC are serialised correctly
with the Z suffix, here's an example.
If I add a select to the query then the Z suffix is lost. http://localhost:50469/efodata/Jobs?&$filter=JobId%20eq%2067&$select=WorkFinished
I'm sure it didn't used to do this but I could be mistaken. Both queries call the same end point, it's only the select that's different.
Aug 05, 2014 03:07 AM|Shawn - MSFT|LINK
By default, Json.NET writes dates in
ISO 8601 format. Dates in UTC (Coordinated Universal Time) are written with a "Z" suffix. Dates in local time include a time-zone offset. For example:
2012-07-27T18:51:45.53403Z // UTC
2012-07-27T11:51:45.53403-07:00 // Local
The suffix ‘Z' (not case sensitive) indicates to use GMT time zone based date time value. Without the suffix ‘Z', the local time zone based date time value will be used in the process that runs the query.
For more information:
Aug 05, 2014 04:41 AM|Ben Crinion|LINK
Yes I understand that. I've deliberately stored all my dates as UTC time zone and ensure that when I load them from the database they are specifically stated as UTC.
If you look at my first example JSON in my original post you can see all the dates, including the WorkFinished date have the Z suffix.
In my second example all I've done is added a $select parameter to my query and the WorkFinished time has lost the Z suffix. The value for the time is the same "17:47:00" but since it's treated as a local time daylight savings isn't applied so my client
application shows the time as an hour out.
Aug 05, 2014 06:13 AM|Ben Crinion|LINK
The reason it's not working is because we set the DateTimeKind by applying an attribute when then object is materialised in the Entity Framework context.
(sender, e) => DateTimeKindAttribute.Apply(e.Entity);
If you use $select the object isn't materialised, so the attribute isn't applied.
Apr 20, 2016 10:07 PM|stickian|LINK
Did you modify this approach to work with $select? If so, what was your solution? Thank you.