1

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

5
  • Can you explain exactly what your problem is? Are you serializing using knockout and deserializing using WebAPI? What does your controller look like? Commented Nov 13, 2012 at 15:23
  • This article gives a good method for replacing the default serializaer so that JSON uses ISO8601 by default: hanselman.com/blog/… Commented Nov 13, 2012 at 17:38
  • @Tyrsius Json.NET uses ISO8601 by default in the version WebAPI uses. This was a change made in Json.NET ~6 months back. Commented Nov 13, 2012 at 18:57
  • @YoussefMoussaoui Ah, I didn't realize JSON.net was already the default. Good to know. Commented Nov 13, 2012 at 21:32
  • One of the problems has been solved - we discovered that the contentType was not set to "application/json" on the POST. That meant that the WebAPI controller now recognizes the dates, but still drops 2 hours Commented Nov 14, 2012 at 12:05

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That's it!! thanks it worked. The problem now is a bigger problem. If someone in India specifies the 15th and the server is sitting in the USA it will convert to local time, which is the 14th. Is that right or wrong? Technically the 15th in India is the 14th in the USA (for some of the day anyway) but does the Indian guy mean the 15th regardless of the timezone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.