Reputation: 3329
I am battling to get knockout to work with ASP.NET Web API specifically when it comes to dates. I have had a look at this question and other similar ones, but I don't like the solution given.
I have an object with some dates in it. When I return it from the Web Api controller it is serialized as follows:
{
"AdjustmentId": "6b8bc63f-de50-4feb-b0e2-a10800cbc3c3",
"PortfolioCode": 2461,
"Description": "",
"StartDate": "2012-11-13T00:00:00+02:00",
"EndDate": "2012-11-13T00:00:00+02:00"
}
When I return the exact same ViewModel from knockout using ko.toJSON, I get the following:
{
"AdjustmentId": "6b8bc63f-de50-4feb-b0e2-a10800cbc3c3",
"PortfolioCode": 2461,
"Description": "",
"StartDate": "2012-11-12T22:00:00Z",
"EndDate": "2012-11-12T22:00:00Z"
}
The problem is that the dates are now not de-serialized on the controller. The object is there, but the dates are set to DateTime.Min. You can also see that the dates have gone from midnight on the 13th to 10pm on the 12th - that's because of the timezone changes.
Can anyone help me on this? At the moment as a work around I am converting the dates using toISOString() but I'd like that to happen in the serialization automatically.
I'd really appreciate any help on this
Upvotes: 1
Views: 835
Reputation: 401
What could be happening here is that when the posted date/time gets deserialized on the server side, it is of kind "UTC", and not "Local". Try converting it to local time with .ToLocalTime() on the server side.
Upvotes: 1