atelion
atelion

Reputation: 71

string not recognized as valid datetime with same format

I have this string: 02/01/2019 13:00:00 I want get only the 02/01/2019, so I did:

var date = DateTime.ParseExact(match.datetime.ToString(), "dd/MM/yyyy hh:mm:ss", CultureInfo.InvariantCulture).Date.ToString();

nb: match.datetime contains the value 02/01/2019 13:00:00

but I get this error:

string not recognized as valid datetime

Upvotes: 1

Views: 84

Answers (2)

shaneOverby
shaneOverby

Reputation: 60

The problem is that in your format ("dd/MM/yyyy hh:mm:ss") you're specifying a 12-hour representation with "hh", but you're input is outside of that range: "13:00:00".

Upvotes: 2

Jonathon Chase
Jonathon Chase

Reputation: 9714

24 hour time is represented by HH for parsing, not hh.

Try something like this:

DateTime.ParseExact("02/01/2019 13:00:00", "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture)

Upvotes: 7

Related Questions