0

I know there are lots of topics talking about how to properly configure the culture and I see other people having the issue of wrong Date formatting in api requests. In Germany we have the following format for dates: dd.MM.yyyy.

This is the request I am sending to my api:

http://localhost:1111/api/Test/Test?datum=07.08.2019

However, the request received inside the controller always displays it like this: 08.07.2019. As you can see the day and month are swapped for some reason.

This is my startup.cs:

public void ConfigureServices() {
    ...
    services            
        .AddMvc()
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
        // Maintain property names during serialization. See:
        // https://github.com/aspnet/Announcements/issues/194
        .AddJsonOptions(options =>
        {
            options.SerializerSettings.Culture = new CultureInfo("de-DE");
            options.SerializerSettings.ContractResolver = new DefaultContractResolver();
            options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
    });
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    var cultureInfo = new CultureInfo("de-DE");

    CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
    CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
}

I am getting very frustrated with this issue and I can't seem to solve it. I know that I can just send the string ISO formatted but this should not be the ideal solution. Please don't show me any workarounds, I am interested in the proper solution and why it isn't working.

I want to send my date in the format dd.MM.yyyy

Tom el Safadi
  • 6,164
  • 5
  • 49
  • 102

2 Answers2

1

Have you tried JsonSerializerSettings.DateFormatString = "dd.MM.yyyy"; ?

REF: JsonSerializerSettings.DateFormatString Property

P.S.: I recommend ISO date formats. Not everyone is located in Germany - how about our Turkish friends?

AlwaysLearning
  • 7,915
  • 5
  • 27
  • 35
  • I will give it a shot, thanks! It’s a business software which has only one department in Germany. So, there are only users from Germany using it. – Tom el Safadi Aug 07 '19 at 12:05
  • That doesn't matter. You should always code in the anticipation of any scenario, especially when it comes to culture and internationalization. What if the company expands to other markets? Then they have to rewrite the software. – Chris Pratt Aug 07 '19 at 12:36
  • @TomelSafadi Generally speaking date/time/number/currency formatting is a presentation issue in the GUI. You should not let that influence the underlying communications stack and storage, both of which should be geared towards machine-readable neutral culture formats. e.g.: our applications are used around the world and present in the culture of the user's desktop/mobile operating system, but date/times are stored in the UTC time zone and are communicated in ISO 8601 formats. – AlwaysLearning Aug 07 '19 at 13:16
1

If you want to set culture for date binding , you could configure the below settings in the ConfigureServices method :

services.Configure<RequestLocalizationOptions>(options =>
{
            options.DefaultRequestCulture = new RequestCulture("de-DE");
            options.SupportedCultures = new List<CultureInfo> { new CultureInfo("de-DE") };
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

Second. Ensure that your call to UseRequestLocalization happens before UseMvc.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseRequestLocalization();
    app.UseMvc();
}

Reference : https://stackoverflow.com/a/44692569/10201850

Xueli Chen
  • 11,987
  • 3
  • 25
  • 36