Search code examples
c#datetimeglobalization

DateTime.Now Date Format and Windows Settings


I have a console application written in C# that makes use of DateTime.Now. With the Windows Region set to "English (United States)", the short date output is M/d/yyyy. I have an instance of my application running on a machine with the culture format set to "English (Canada)" and the short date format set to dd/MM/yyyy. Since I want consistency within my application across different servers, I changed the short date format in Windows' Region settings to M/d/yyyy. However, my application is still outputting DateTime.Now as dd/MM/yyyy. Is there something else that needs to be changed for my application to output in the format I specified?

I do this in various places but here is an example:

TimeZoneInfo customTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime thisTime = TimeZoneInfo.ConvertTime(DateTime.Now, customTimeZone);
//The below output is not formatted how the Windows Region short date is specified.
Console.Writeline(thisTime);

In this case, my DateTime is not formatted how its specified in the Windows Region settings.


Solution

  • You can change the culture of the current thread as follows on application start. This affects the date format, currency format, etc.

    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    

    See MSDN: Globalization for more information (see section on Dates and Times).