3

I have a fairly simple asp.net core (MVC) project, which includes some date fields in the data.

I want the dates to be displayed as dd/MM/yyyy, so the date fields have been given the displayformat as such:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? AccessExpiresDate { get; set; }

When testing in my windows dev environment, all is good. If I want to enter the 1st of december 2018, I enter 01/12/2018, and it saves as such. I go back in, and it displays as such again (01/12/2018).

But when I deploy to linux (Centos), it behaves differently. It displays correctly (dd/MM/yyyy), but seems to be need to be entered as MM/dd/yyyy to edit.

For example, if I enter the date as 01/12/2018, save, then view, it will display as 12/01/2018. If I go back to edit, it is displayed as 12/01/2018. If I then save it without changing, then go to view again, it displays as 01/12/2018.

If I enter the date as 20/12/2018, it will null out the date, as if that is an invalid date (as it thinks I am entering the 20th month).

So - whats going on here? Why doest the date entry behave differently when deployed on linux? How can I force it to always expect the date to be dd/MM/yyyy?

Thanks.

Shaggs
  • 111
  • 3
  • 11

1 Answers1

1

I found the answer in this posting: How to set culture for date binding in Asp.Net Core?

I put the following code int he start of Startup.Configure, and all was good:

    var supportedCultures = new[] { new CultureInfo("en-AU") };
    app.UseRequestLocalization(new RequestLocalizationOptions
    {
        DefaultRequestCulture = new RequestCulture("en-AU"),
        SupportedCultures = supportedCultures,
        SupportedUICultures = supportedCultures
    });
Shaggs
  • 111
  • 3
  • 11