Tryparse datetime string for date and time format

I am trying to parse a "31.01.2017 07:56:29.470000000"date time string in datetime format.

Used code:

   DateTime requiredDate;
    string date = "31.01.2017 07:56:29.470000000";

    DateTime.TryParseExact(date,
                           "dd.MM.yyyy hh:mm:ss.fffffff",
                           CultureInfo.InvariantCulture,
                           DateTimeStyles.None,
                           out requiredDate);

NOTE: the date string is exact "01/31/2017 07: 56: 29.470000000", however, if I use "01/31/2017 07: 56: 29.4700000", then it works fine.

Please analyze "01/31/2017 07: 56: 29.470000000".

+4
source share
2 answers

The problem is the maximum number fin your parsing line: maximum value fffffff(7 fractions). Your line contains 9 of them.

. f fffffff, .

+5

f, 7. , ISO 8601. . .

"O" "o" "yyyy" - "MM" - "dd'T'HH": 'mm': 'ss'. ' fffffff K " DateTime " yyyy '-' MM '-' dd'T'HH ':' mm ':' ss '.' fffffff zzz " DateTimeOffset.

, , , 7 f.

, 2 .

+2

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


All Articles