NibblyPig
NibblyPig

Reputation: 52932

TryParseExact returns false when the string seems to match the pattern

I have this format string "YYYY-MM-dd HH:mm:ss"

I have this date: "2013-08-28 15:01:17"

And this is my code:

DateTime.TryParseExact(fromString, dateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out outDateTime);

However it returns false indicating it failed to parse.

I'm not really sure why, any ideas?

Upvotes: 2

Views: 152

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 148990

YYYY is not a valid format specifier (it would be parsed as a literal YYYY). Use lowercase y's in your format string instead:

yyyy-MM-dd HH:mm:ss

Further Reading

Upvotes: 6

Related Questions