5

I have a Winform client that sends a json post request to my controller with a datetime value of the format dd/MM/yyyy and the call returns a status code 400.

I tried to add:

<globalization uiCulture="fr" culture="fr-FR" />

to my web.config file but it's still not working.

Edit: I should add also That I have no control over the client and that I added:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/mm/yyyy}")]

to my model with no result

asked Dec 11, 2012 at 9:20

4 Answers 4

11

There's a pretty simple solution. Just add the CultureInfo to the JsonSerializerSettings in global.asax, method Application_Start().

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = 
 new JsonSerializerSettings
 {
 DateFormatHandling = DateFormatHandling.IsoDateFormat, 
 DateTimeZoneHandling = DateTimeZoneHandling.Unspecified,
 Culture = CultureInfo.GetCultureInfo("fr-FR")
 };
answered Nov 12, 2013 at 21:57

1 Comment

How do you do it in .net core?
3

If you are posting this via JSON then you should be able to create a JSON.NET converter for your date format.

In fact in researching this answer I found this full example on SO WebApi Json.NET custom date handling

Obviously just alter the conversion in MyDateTimeConvertor to be something that uses the current culture and the format you spefified.

DateTime.ParseExact(reader.Value.ToString(), "dd/mm/yyyy", CultureInfo.CurrentCulture);

AND

writer.WriteValue(((DateTime)value).ToString("dd/mm/yyyy"));
answered Dec 11, 2012 at 14:47

Comments

1

before:

"AccessStartTime": "2020年12月01日T00:00:00",

add below code into Application_Start() of Global.asax.cs

 IsoDateTimeConverter converter = new IsoDateTimeConverter
 {
 DateTimeStyles = DateTimeStyles.AdjustToUniversal,
 DateTimeFormat = "yyyy-MM-dd HH:mm:ss"
 };
 GlobalConfiguration.Configuration.Formatters
 .JsonFormatter.SerializerSettings.Converters.Add(converter);

after:

 "AccessStartTime": "2020年12月01日 00:00:00",

reference: https://stackoverflow.com/a/42816827/2736970

answered May 18, 2021 at 8:43

Comments

1

One thing I noticed is you have a bug in attribute you specified for your model:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/mm/yyyy}")]

This:

{0:dd/mm/yyyy}

Is not the same thing as:

dd/MM/yyyy

mm - for minutes, MM - for months.

answered Aug 23, 2022 at 0:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.