2

I submit JSON object, with date value "02/11/2015" (DD/MM/YYYY) to API controller. For some reason it gets converted into US format while binding (11th of Feb). I have standard MVC controllers in the same solution, which bind date value submitted in the same format correctly (UK). What would be causing this difference:

Here are standard MVC submission headers:

Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8,pl;q=0.6
Cache-Control:no-cache
Connection:keep-alive
X-Requested-With:XMLHttpRequest

And Form Data

StartDate : 01/11/2015

Here Are API call headers

Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8,pl;q=0.6
Cache-Control:no-cache
Connection:keep-alive
Content-Type:application/json; charset=UTF-8
X-Requested-With:XMLHttpRequest

And JSON object that I pass

{ DateStart: "01/11/2015" }

1 Answer 1

0

I guess the default dateFormat in webapi json serializer is not the same as MVC's one.

Try to specify in your WebApi in the config file (App_start/WebApiConfig).

Example:

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;

Or GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.DateFormatString = "yyyy-MM-dd"

with the DateFormat, DateZone you need.

More infos here:

Post dates web api custom format

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

3 Comments

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.DateFormatString = "yyyy-MM-dd" in the register method of your webapiconfig file does not change the format?
Made it work finally using custom converter for DateTime type: config.Formatters.JsonFormatter.SerializerSettings.Converters.Insert(0, new MyDateTimeConverter()); Thanks for your help

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.