petko_stankoski
petko_stankoski

Reputation: 10713

datetime.parseexact returns wrong month

Here is my code:

a.dateFrom = DateTime.ParseExact(x, "dd/mm/yyyy", null);

And x has value of: 08/03/2012

However, a.dateFrom has value of 08/01/2012. Why?

Upvotes: 6

Views: 2912

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500495

As ionden notes, you should have a format of

"dd/MM/yyyy"

Currently you're parsing the second part as minutes (as that's what mm means).

See the documentation for custom date and time format strings for more information. I'd also strongly encourage you to consider using the invariant culture for parsing - if you're using a custom format string, that usually means you don't want to treat the input in a culture-sensitive fashion at all.

Upvotes: 5

ionden
ionden

Reputation: 12776

You should use MM as format for month

Upvotes: 21

Related Questions