2

I have a very odd one here.

In a simple Windows Form application i write the following C# Code

MessageBox.Show("Todays date is: "+DateTime.Today.ToShortDateString());

Running this normally just show the date in the format specified by windows (in the region settings). (so in my machine set to danish local it show the date as dd-MM-yyyy => 13-10-2017) This is expected.

But I have a customer's server (Windows Server 2012) where this is not the case. In their setup the Windows region settings is like mine (dd-MM-yyyy) but despite that the simple above code show it as MM/dd/yyyy => 10/13/2017 which is the American format.

I simply do not understand this and to make it even more of a mystery it is only for some windows users on this server (some show the format as the clock in window and other with the invariant format).

I've been searching on Google if there is some sort of .NET setting that can override on user-level but have not found anything.

There is nothing in the code that se culture and anything special (just File > New project > Winform [Targeting .net 4.5] and the line of code above ).

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
RWJ
  • 435
  • 5
  • 10
  • Do you want to respect windows settings, e.g. what should happen if user have `en-US` selected there? You can [force danish locale](https://stackoverflow.com/q/468791/1997232). – Sinatr Oct 13 '17 at 09:45
  • I do not wan't to force anything here. I just wish that my program will follow what is selected in Windows region settings – RWJ Oct 13 '17 at 09:49
  • Is the code executing on that server? And is the actual region for that server set correctly, or just a display format? The two can be different. – DonBoitnott Oct 13 '17 at 10:50
  • 2
    One thing I've noticed when it comes to locales like this is that if your application is running as a service, and the service is running as i.e. "system" account, then it won't use the locale defined in Windows as you really can't log on as the system account and change the region. This can be solved by running the service as a specific local account where the region has been set to the required locale. – TheHvidsten Oct 13 '17 at 11:23
  • It is running directly on the server. Don't know of the actual region setting, but still can't see that even if that how Windows clock and Program can show 2 different formats – RWJ Oct 13 '17 at 11:24
  • @GTHvidsten In this case it is just the normal login user that run the sample exe. So can't see it is running as another user – RWJ Oct 13 '17 at 11:26
  • @RWJ Just double checking here, but does your application require elevated privileges? If it is running as administrator it will also not use the regional settings for the logged in user. – TheHvidsten Oct 13 '17 at 11:31
  • @GTHvidsten It does not use any elevated privileges (just a small test winform (double-click exe as normal)) – RWJ Oct 13 '17 at 13:14

1 Answers1

1

Have you tried using the ToString() method?

DateTime.Today.ToString(CultureInfo.InvariantCulture)

Result: 10/13/2017 00:00:00

DateTime.Today.ToString(CultureInfo.CurrentCulture);

Result: 13.10.2017 00:00:00

This is what I experience on my (German) machine.

Neuron
  • 5,141
  • 5
  • 38
  • 59
  • ToShortDateString also do culture and if no culture specified it is current culture. Problem where is that default is wrong (invariant) despite seeing it being used in Windows Taskbar by the clock – RWJ Oct 17 '17 at 11:00