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?