I have a date time in the database and I retrieve it from the database using Entity Framework, I then pass out the data via JSON API through the DataContractJsonSerializer.
The time in the date time field appears to have been adjusted according to the local timezone of the server whilst being processed in DataContractJsonSerializer. The epoch expressed time is 1 hour ahead of the time expected. The DateTime Kind is UTC, but previously it was Unspecified and I had the same issue.
In my application, I wish to convert between timezones explicitly and on the client side, not the server, as this makes more sense. I’m surprised at this implicit functionality as my datetime values should be simple values just like an integer.
thanks
DataContractJsonSerializerwill output the timezone portion (+zzzz) if your DateTime.Kind is equal to Local OR Unspecified. This behaviour differs from the XmlSerializer which only outputs the timezone portion if Kind equals Unspecified.If curious check out the source for
JsonWriterDelegatorwhich contains the following method:I’ve run the following test on my machine
which produces the expected output:
Thus at some point your Date must be Unspecified or Local.
After pulling it out of the DB convert the kind from Unspecified to Utc by calling
and don’t forget to assign the return value of
SpecifyKindback into your object like I have