views:

32

answers:

2

I converted my VS 2008 project into vs 2010 but kept it on .NET 3.5 framework. I don't set my locale anywhere within the app.

I've got a couple on computers running windows 7 and XP and both have the region set to EN-AU.

Sometime my app returns the short date format like MM/dd/YY (EN-US). As soon as you quit it and start again it reverts to the proper format for AU(dd/mm/yy). Again I'm not setting my locale anywhere in the project. (I have some RDLC reports which have EN-US as the language though)

Any reason why this would be happening? I tried setting the Thread locale explicitly as well. But same behaviour.

+3  A: 

Datetime.UTC is based on local system time and whether the local system is observing daylight savings time.

Regardless whether you set your location. The datetime picker should automatically set itself based on UTC and your system.

Check out this link which gives some helpful hints to datetime

http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/6b3b1e95-e044-46db-94ba-0e75fcf9d2b2/

Michael Eakins
But it shouldn't use AUS format sometimes and US format the other.
Dasiths
I agree, it should always use your system default setting.
Michael Eakins
A: 

This is strange. I would advice you not to use ToShortDate() as I have seen couple of issues from it. Instead, I recommend using following code:

string formattedDate = someDateTime.ToString("d", CultureInfo.CurrentCulture);

This basically does the same. And you can introduce constant instead of "d" for greater readability (it is short date formatting string).

It is always good practice to pass IFormatProvider, as it works as a comment clearly documenting your assumptions (in the example above I said: this is the date string I want to present to user; if I wanted to use this date for further processing, send it via network, etc. I would use CultureInfo.InvariantCulture).

Paweł Dyda
I'm using datagridview's and setting column format as "d", also same results. It uses AUS format sometimes and US format sometimes.
Dasiths
I observed the current culture and even tried setting in explicitly. Except for when viewing RDLC/Reports the culture of any given thread is always en-au.
Dasiths
what are the issues with ToShortDate() you mentioned?
Dasiths
The issues with ToShortDate() does not apply to your problem actually. The problem was, we need to pass short date string to some 3rd party code which expected it to be in InvariantCulture format. Since there is no overloaded version of ToShortDate() that takes IFormatProvider, we ended up with nasty exception on some locales.
Paweł Dyda
From what I know about DateTime's To(...)String() methods implementation, I am pretty sure that something (possibly RDLC/Reports?) is changing your Thread's CurrentCulture along a way. There is no obvious solution for such issues other than removing defective component. You can of course try to always format date with reference to your desired culture, which simply cannot fail, but it seems it will be very problematic (especially if you have to do that for DataGridView's data).
Paweł Dyda