DateTime formatting for fr-CA
I just stumbled across a nice little hidden “feature” in the .NET framework. If you’re running on a machine that has the CurrentCulture set to fr-CA the default DateTimeFormatInfo.CurrentInfo.ShortDatePattern is dd-MM-yyyy. On my current project we wanted to allow the end user to override that value with their own format when a date is displayed on the screen. The easy way to do this is to do something like DateTime.Now.ToString(“dd/MM/yyyy”). Unfortunately the result from that will appear as 16-09-2010 still. As far as I can tell (and there is very little backing this up), this is by design. I’m not sure why at all. If the CurrentCulture is set to en-CA both the formats of dd/MM/yyyy and dd-MM-yyyy will cause ToString() to output a value that you would expect, but as soon as you trip over to fr-CA the rules seem to change.
If you’re running into this there is a relatively simple solution. DateTime.Now.ToString(“dd\/MM\/yyyy”) will output 16/06/2010 as you’d expect.
The more localization that I’m doing on this application, the more I’m finding nice hidden gems of inconsistency like this.
Comments
What about using the InvariantCulture format provider in those instances?
DateTime.Today.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)
Good call on this. Tried it this morning and it seems to have worked the trick. Thanks William.
Great, tried this trick and worked.