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!!