Date formatting in C #

This is a problem that I have encountered several times, and I am sure that there is an elegant solution that I am missing.

I have some DateTime variables in C # that I get from different 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 of them are set up as English (British). I do not control some of them, since I would very much like to put them all in one culture or another.

So far, I have converted them using the CultureInfo object in order to format it correctly, for example:

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

However, it only recently seemed to me that the conversion does not know in what culture the original DateTime is located (as I said, it can be both American and British), since this is just a string.

In the case of a date string, for example, “06/15/2009”, this is normal because the conversion recognizes that “15” cannot be a month. However, the date string "06/07/2009" will always be valid, but depending on whether the original was American or British, this may refer to different days and months.

Is there a more acceptable method for handling DateTime in general that reduces these ambiguities? Thanks.

EDIT: That's right, so there seems to be no reliable way to always convert to the correct format, because my information is limited.

DateTime .dll. . , SQL, .dll , . ( ), DateTime ? , , ?

+3
3

: . , - , , .

.

+7

( ):

  • TryParse,
  • , Parse
0

, - - , , . DateTime #, doesn ' t matter - .

, - ?

0

Source: https://habr.com/ru/post/1715426/


All Articles