tags:

views:

248

answers:

3
+3  Q: 

C# date formatting

Hi there. This is a problem I have come across a number of times and I'm sure there's an elegant solution I'm missing.

I have some DateTime variables in c# that I get sent from various SQL tables/websites/web services, often sent as strings. The problem is that some of these sources are set up as English (United States) and some are set up as English (British). I don't have control over some of these, as I would dearly like to set them all to one culture or the other.

Until now I have been converting these using a CultureInfo object to format it correctly like:

CultureInfo ci = new CultureInfo("en-GB");
Convert.ToDateTime(inputDateTimeString, ci);

However, it has only recently occured to me that the conversion doesn't know what culture the original DateTime is in (as I said, it could be either American or British) since it is just a string.

In the case of a date string, say "06/15/2009", this is fine because the conversion recognises that '15' can't be the month. However a date string of "06/07/2009" will always be valid but, depending on whether the original was American or British, it could be referring to different days and months.

Is there a better accepted method for handling DateTime's in general that cuts down on these ambiguities? Thanks.

EDIT: Right, so it seems there is no reliable way to always convert into the correct format because my information is limited.

One source of these DateTime strings is a .dll I have no control over. However, I DO have control over the SQL login that this .dll uses to access the database where the information is stored. If I were to change the language setting of this login to British English (it is currently American English), would it retrieve DateTime's in that format, or would it have no effect? I'd have to check ofcourse that it didn't screw anything else up, but might it work?

+7  A: 

You need to tackle the problem at its source: you're being given data in an ambiguous format. If you can't change the data sources themselves, you should decorate them with a format string or something like that, so that you know to treat the whole data source in one way or the other.

Without that information you simply can't reliably parse the data.

Jon Skeet
Well said. This problem reminds me of being told to split names (first,last) after the fact, with no supporting data. You can do it... but it won't be reliable.
Russell Steen
If only all systems handled date time in UTC format and only converted at display time.
Gus Paul
@goap: That doesn't actually solve all problems. Sometimes you need to know the local time + time zone. For instance, you can't work out "same time next week" without knowing the time zone.
Jon Skeet
A: 

What I usually do (with any parsing) is:

  1. TryParse using the culture of the user
  2. If it fails, Parse using the culture invariant flag
GoodEnough
Use the value you got from the parsing...
GoodEnough
A: 

If I understand your question correctly it is a bit out of your control - if you get these dates from external sources, you need to know what format they are in. Once you get a DateTime object in C# the initial format doesn't matter - it just contains the date and time regardless of the format.

On the other hand why don't you get them as some sort of date time database format from the database?

Grzenio