Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

datetime.parseexact returns wrong month

Tags:

c#

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?

like image 843
petko_stankoski Avatar asked Mar 27 '12 11:03

petko_stankoski


2 Answers

You should use MM as format for month

like image 116
ionden Avatar answered Oct 23 '22 01:10

ionden


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.

like image 44
Jon Skeet Avatar answered Oct 23 '22 00:10

Jon Skeet