[RESOLVED]How to accept ISO date format in Web API

jquery $.ajax() sends dates from brower in ISO format: yy-mm-dd or yyyy-mm-dd if this is better in POST body.

MVC4 Web API controller contains DateTime property for this.
How to get date value in any server thread culture settings.

I tried

public class TestController : ApiController {

public class PlaaniSisend
public DateTime date { get; set; }

public HttpResponseMessage Post([FromBody] PlaaniSisend vali)
Console.Write(vali.date );

If server thread culture date format is different than ISO, vali.date value is wrong.

How to fix this so that ISO date format is uset for parse in any server culture settings ?

you can achieve it by using model binding

when post action receiving data in forms of models, then manipulate in model binding

Is’nt there a simpler way ?

There can be lot of date properties in nested object structure. Thi sis lot of coding.

Newtonsoft.Json used by Web API has ISOFormatter class ? Maybe it is possible to forze serializer/and deserializer to use ISO date format in MVC4  ?

Or is it possible to changed thred culture to Invariant  during serialization and deserialization ?

Please take a look at this blog, it shows how to use model binging for custom date formats. You can extend this for different cultures.

Custom Date Formats and the MVC Model Binder

Leave a Reply