The date time for the code below looks different on Windows 10 and Linux (Ubuntu 22.04.2):
DateTime date1 = new DateTime(2001, 9, 1);
Console.WriteLine(date1.ToString());
Windows date time format: 01.09.2001 00:00:00
Ubuntu date time format: 09/01/2001 00:00:00
Question: How to set the global date format in an asp.net application regardless of the date format set in the system the application is running on?
I tried setting the culture in web.config but it doesn’t help.
<system.web>
<globalization culture="pl-PL" uiCulture="pl-PL"/>
</system.web>
I tried setting the culture in Program.cs but it doesn’t help either.
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
options.DefaultRequestCulture = new RequestCulture("pl-PL", "pl-PL");
options.SupportedCultures = new[] { new CultureInfo("pl-PL", false) };
options.SupportedUICultures = new[] { new CultureInfo("pl-PL", false) };
});
EDITED:
I corrected the code:
var app = builder.Build();
app.UseRequestLocalization(options =>
{
options.DefaultRequestCulture = new RequestCulture("pl-PL", "pl-PL");
options.SupportedCultures = new[] { new CultureInfo("pl-PL", false) };
options.SupportedUICultures = new[] { new CultureInfo("pl-PL", false) };
});
After applying the fix above, the date format is compatible with the set culture, but the problem persists, i.e. the date format is still different on Windows and Linux despite the set culture:
Windows date time format: 01.09.2001 00:00:00
Ubuntu date time format: 1.09.2001 00:00:00
Is it normal for Ubuntu to have a missing zero at the beginning of the days of the month from 1 to 9, or is it a bug?
2
Answers
According to the information in the Date formatting discrepancy between Windows and Linux, it's normal that there may be differences in the date formats used on different systems. If we want the date format to be independent of the client's system, we should take care of it from the application side.
Try to specify the culture info for
ToString
: