nickand
nickand

Reputation: 127

How do i stop Entity Framework converting Json UTC datetime when i want to store UTC datetime

Banging my head against the wall on this issue.

I receive a text string to my web service which is generated with json, for example:

"2014-09-19T17:00:00.000Z"

When i assign this to my database in entity framework, it changes it to 20:00 instead of 17:00. So i specify that it is UTC date by

DateTime.SpecifyKind("2014-09-19T17:00:00.000Z", DateTimeKind.UTC)

But this returns 20:00 as well! I could fix this by removing the Z but that is a dirty workaround as could be receiving time zones instead of Z. Is there way of setting telling vb to ignore the timezone and just save it as 17:00?

Upvotes: 2

Views: 1147

Answers (2)

nickand
nickand

Reputation: 127

Thanks to Pawel for the hint. To help anyone else with this problem, please see the code below:

New DateTimeOffset("2014-09-19T17:00:00.000Z").ToUniversalTime

Upvotes: 1

Pawel
Pawel

Reputation: 31620

As described here EF will always read the date time value as if it was of Unspecified kind since DateTime does not store time zone. Consider using DateTimeOffset which does store time zone.

Upvotes: 2

Related Questions