fingers10
fingers10

Reputation: 7927

How to set date formats and culture in asp.net core razor pages 2.2

I'm using Asp.Net Core 2.2 Razor Pages application. I want to set application culture and date formats in application level. So after some googling I end up adding the below code in ConfigureServices method in Startup.cs

public void ConfigureServices(IServiceCollection services)
        {
            // setting the application culture to en-US with MM/dd/yyyy short date pattern.
            var culture = CultureInfo.CreateSpecificCulture("en-US");
            var dateformat = new DateTimeFormatInfo { ShortDatePattern = "MM/dd/yyyy", LongDatePattern = "MM/dd/yyyy hh:mm:ss tt" };
            culture.DateTimeFormat = dateformat;
            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;

But this doesn't seem to work. I'm getting the date in wrong format. Example, DateTime.Now.ToShortDateString() returns 6/25/2019 instead of 06/25/2019. The same is the problem with DateTime.Now.ToLongDateString()

And If I change the format to "dd/MM/yyyy" will it return the date in 25/06/2019??

I'm running this in my local development environment. Also what I'm expecting is that my app to get the dates in the same format I set above in any environment, any machine, any culture.

Please assist and guide me on where I'm wrong.

Thanks in advance!!

Upvotes: 4

Views: 17153

Answers (2)

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131180

There are two questions here :

  1. How to properly localize a web appication and
  2. How to use a custom culture ?

ASP.NET Core and ASP.NET offer their own globalization and localization features that work on multiple levels (application, page, request). You should check Globalization and Localization in ASP.NET Core to understand how these features work in ASP.NET Core. As everything else, the services are provided through middleware.

There are a lot of blog posts about this too. I picked this one due to the title Internationalization - ASP.NET Core: From 0 to overkill. It explains the same things the docs do, in a more concise way.

For your specific question though, all that's needed is to set the culture for each request. This can be done with the localization middleware's UseRequestLocalization method.

In Startup.cs's Configure(IApplicationBuilder, IWebHostEnvironment), right before app.UseStaticFiles() you can add a call to UseRequestLocalization to specify the cultures :

var supportedCultures = new[]
{
   new CultureInfo("ru-RU"),                

};

app.UseRequestLocalization(new RequestLocalizationOptions{
    DefaultRequestCulture = new RequestCulture("ru-RU"),
    SupportedCultures=supportedCultures,
    SupportedUICultures=supportedCultures
});

I picked ru-RU because it uses a dot for a date separator. Putting

@DateTime.Today.ToString()

in a Razor page this produces

25.06.2019 0:00:00

You can pass your own custom culture to UseRequestLocalization too. Using this custom culture :

var culture = CultureInfo.CreateSpecificCulture("en-US");
var dateformat = new DateTimeFormatInfo { 
    ShortDatePattern = "MM/dd/yyyy", 
    LongDatePattern = "MM/dd/yyyy hh:mm:ss tt" 
};
culture.DateTimeFormat = dateformat;

var supportedCultures = new[]
{
    culture
};

app.UseRequestLocalization(new RequestLocalizationOptions{
    DefaultRequestCulture = new RequestCulture(culture),
    SupportedCultures=supportedCultures,
    SupportedUICultures=supportedCultures
});

Produces :

06/25/2019 00:00:00

Upvotes: 17

koviroli
koviroli

Reputation: 1453

Your WebApplication use multiple threads, if you set a CurrentCulture or CurrentUICulture of your current thread, others may differ.

Try use DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture :

CultureInfo.DefaultThreadCurrentUICulture = culture;
CultureInfo.DefaultThreadCurrentCulture = culture;

Upvotes: 3

Related Questions