简体   繁体   中英

C# date formatting

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?

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.

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM