views:

159

answers:

2

On a 32 bit OS, with an Intel processor, DateTime e.g. 2/17/2009 12:00:00 AM Notice that it is: mm/DD//yyyy

On a 64 bit OS, with an AMD processor, DateTime e.g. 17-02-2009 00:00:00

Now when I try to parse the 1st format, it throws an error on the 2nd platform. That means - DateTime.Parse("2/17/2009 12:00:00 AM") - throws an error - cannot convert. whereas, on the same platform, DateTime.Parse("17/2/2009 12:00:00 AM") works! That means DD/MM is fine, MM/DD is not.

What is causing this? The 64-bit OS? The processor?

How do I get rid of the problem?

+5  A: 

DateTimes themselves don't have formats. You parse them or format them into strings. (It's like numbers - integers aren't stored in hex or decimal, they're just integers. You can format them in hex or decimal, but the value itself is just a number.)

The format will depend on the culture of the operating system (or more accurately, the culture of the thread, which is typically the same as the operating system one).

Personally I like to explicitly set the format I use for either parsing or formatting, unless I'm actually displaying the string to the user and know that the culture is appropriate already.

Jon Skeet
I know that FxCop warns you when you do this sort of thing with strings. Does it do the same for DateTime?
Roger Lipscombe
@Roger: No idea... I really should take a closer look at FxCop at some point...
Jon Skeet
+2  A: 

Check your "Date and time formats" in the "Region and Language" control panel.

Also, if you want DateTime to generate a specific format, don't just call plain ToString(), but pass it a parameter indicating the format you want. Similarly, if you know the format of the date you are asking it to parse, call TryParseExact(), and tell it the format you are providing.

See also MSDN's Standard Date and Time Format Strings

Chris W. Rea