When I create a Buyin
object the response from the ASP.NET MVC controller, (return Json(response, JsonRequestBehavior.AllowGet);
, looks like this:
"Buyin": {
"Id": 95,
"PlayerSessionId": 88,
"PlayerId": 45,
"PlayerName": "Alan",
"Amount": 888,
"BuyinType": "Credits",
"Description": null,
"Authorized": true,
"SignPath": "~/Signs/Buyins\95.png",
"Payment": null,
"CreationDate": "/Date(1477242738042)/"
},
If I convert that on Epoch Converter I get this time: GMT: Sun, 23 Oct 2016 17:12:18.042 GMT
Looking in the database the stored datetime seems to be correct:
95 NULL 1 1 2016-10-23 17:12:18.043
When the response is sent out the Kind
is set to UTC
.
Now I call a controller to get all my data and all of the dates have several hours added to it:
{
"Id": 95,
"PlayerSessionId": 88,
"PlayerId": 45,
"PlayerName": "Alan",
"Amount": 888,
"BuyinType": "Credits",
"Description": null,
"Authorized": true,
"SignPath": "~/Signs/Buyins\95.png",
"Payment": null,
"CreationDate": "/Date(1477267938043)/"
}
1477267938043
= GMT: Mon, 24 Oct 2016 00:12:18.043 GMT
However when I request this object I can see that the actual object has the correct date set:
But the Kind
is set to Unspecified
so I think this is causing the problem.
For the moment I don't have not set any globalization settings.
So basically my question is: When ASP.NET MVC loads the dates from the database is there a way to tell the server to load the dates with Kind
set to UTC
as I think that is the problem?
The database is saved and loaded using Entity Framework.
Update after the accepted answer
The accepted answer was great however my date values was already stored in the Database as UTC dates so I modified GetDateTime
to this:
public override DateTime GetDateTime(int ordinal)
{
var date = base.GetDateTime(ordinal);
var utcDate = DateTime.SpecifyKind(date, DateTimeKind.Utc);
return utcDate;
//return base.GetDateTime(ordinal).ToUniversalTime();
}
See Question&Answers more detail:
os