Home > database >  Change culture info in ASP.NET Core 6
Change culture info in ASP.NET Core 6

Time:06-29

In my project (.NET 6), I take the date from the user and save it in SQL Server, but when I take that date from the database, it displays as a Persian calendar.

For example:

  • Date in the database: 2022-06-28
  • Date taken from the database: 1401/04/07

enter image description here

I tried this way in program.cs but it didn't work:

builder.Services.Configure<RequestLocalizationOptions>(options =>
{
    options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en-US");
    options.SupportedCultures = new List<CultureInfo> { new CultureInfo("en-US")};
});

PS: The culture of my system is Persian

How can I fix it?

CodePudding user response:

Try to use this method, in my code, it works fine.

var supportedCultures = new[]
{
 new CultureInfo("en-US"),
 new CultureInfo("fr"),
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
    DefaultRequestCulture = new RequestCulture("en-US"),
    // Formatting numbers, dates, etc.
    SupportedCultures = supportedCultures,
    // UI strings that we have localized.
    SupportedUICultures = supportedCultures
});

Add this code before app.UseStaticFiles();

Test Result: enter image description here

  • Related