0

I am trying to parse the following format to a datetime:

11/06/2020 - 18:13

I tried:

DateTime t= DateTime.ParseExact(data[i][j], "dd/MM/yyyy - hh:mm", CultureInfo.InvariantCulture);

but it gives exception:

System.FormatException: 'String was not recognized as a valid DateTime.'

spez
  • 409
  • 8
  • 21
  • 1
    Does this answer your question? [DateTime.ParseExact() does not grok 24-hour time values?](https://stackoverflow.com/questions/11232385/datetime-parseexact-does-not-grok-24-hour-time-values) The exact duplicate as well as [How to Parse a DateTime String to Support 24 hours timing?](https://stackoverflow.com/questions/17107230/how-to-parse-a-datetime-string-to-support-24-hours-timing) – Pavel Anikhouski Jun 18 '20 at 09:29

1 Answers1

5

You need to specify 24 hour time HH

DateTime.ParseExact(input, "dd/MM/yyyy - HH:mm", CultureInfo.InvariantCulture);

The "HH" custom format specifier

The "HH" custom format specifier (plus any number of additional "H" specifiers) represents the hour as a number from 00 through 23; that is, the hour is represented by a zero-based 24-hour clock that counts the hours since midnight. A single-digit hour is formatted with a leading zero.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141