简体   繁体   中英

C# Parse DateTime using DateTime.TryParseExact() multiple format

I have a string text that i want to parse/convert to DateTime,
the text can have a different formats (depending on the user who write it),
it can be "dd/MM/yyyy" or it can be "MM/dd/yyyy".

I am using DateTime.TryParseExact(string, formats[], provider, dateTimeStyles, out result)
as descripted here TryParseExact

Now the problem happens in the following cases
if (for example) string text = "06/01/2023"

The text could have two meanings
1 st --> Day: 06 , Month: 01 , Year: 2023
2 nd --> Day: 01 , Month: 06 , Year: 2023

My Question is when does the Method TryParseExact() return true
does it return true for the first format that matches text ?
or does it evalute all/each format inside the formats[] array?

i have tried to search for a similar question, but i didn't seem to find one,
i have tried read this reference TryParseExactMultiple
i have tried this question

Thank you for your help in advance.

My code:

string text = "06/01/2023";

string[] formatsArray = new string[]
{
  "yyyyMMdd",
  "dd/MM/yyyy",
  "dd-MMM-yy",
  "MM/dd/yyyy",
  "MM-dd-yyyy"
};

DateTime result;

DateTime.TryParseExact(text, formatsArray, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out result)

Output is:
result = Day: 06, Month: 01, Year: 2023

It seems that source code is pretty clear - TryParseExact calls TryParseExactMultiple which cycles formats in provided order and returns first one that succeeds:

internal static bool TryParseExactMultiple(ReadOnlySpan<char> s, string?[]? formats,
    DateTimeFormatInfo dtfi, DateTimeStyles style, scoped ref DateTimeResult result)
{
    // ...

    // Do a loop through the provided formats and see if we can parse successfully in
    // one of the formats.
    for (int i = 0; i < formats.Length; i++)
    {
        string? format = formats[i];
        // ...
        if (TryParseExact(s, format, dtfi, style, ref innerResult))
        {
            result.parsedDate = innerResult.parsedDate;
            result.timeZoneOffset = innerResult.timeZoneOffset;
            return true;
        }
    }

    result.SetBadDateTimeFailure();
    return false;
}

Since documentation does not specify behaviour for I would not recommend to rely on it, if it is possible, or possibly wrap this into helper method which will be tested for your particular requirements.

In general - when accepting data from somewhere you need to agree on data format, if the source of the data is not under your control - contact the vendor to clarify it otherwise sooner or later the situation like this are bound to happen (in theory not only related to dates).

According to the documentation , TryParseExact does not provide an indication for which format was used. My general recommendation is to allow users to send only one date format.

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