11

Model binding for date in .net core 2.1 is setting date value in American format even though culture info is set as British format.

Any idea ?

Here is my startup file code snippet

 var  enGbCulture = new CultureInfo("en-GB");
            CultureInfo.DefaultThreadCurrentCulture = enGbCulture;
            CultureInfo.DefaultThreadCurrentUICulture = enGbCulture;

            app.UseRequestLocalization(new RequestLocalizationOptions
            {
                DefaultRequestCulture = new RequestCulture(enGbCulture),

                SupportedCultures = new List<CultureInfo> { enGbCulture },

                SupportedUICultures = new List < CultureInfo > { enGbCulture },
                RequestCultureProviders = new List<IRequestCultureProvider>
                    {
                    new QueryStringRequestCultureProvider { },
                    new CookieRequestCultureProvider { },
            },
            });
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

and below is printing correct culture info in view

 <div>Current Culture: @CultureInfo.CurrentCulture.DisplayName</div>
 <div>Current UI Culture: @CultureInfo.CurrentUICulture.DisplayName</div>
 <div>Current Thread Culture: @Thread.CurrentThread.CurrentCulture.DisplayName;</div>

as enter image description here

Sample Project is on GitHub

2 Answers 2

0

Just show your date in the format you want on the frontend after you will get the date from any input field just convert it to British format.

Try this code to change the date format.

 string date = model.DateOfBirth.tostring(); 
    //convert datetime to string and pass it to following code
    DateTime.ParseExact(date, "M/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
-1

Raised this issue in GitHub CaseRaisedInGitHub and created a demo project to explain the issue as well at https://github.com/nabinkjha/DateTimeLocalization/tree/master/GetSearchDemo

DemoProjectLink

Not the answer you're looking for? Browse other questions tagged or ask your own question.