BaDr Amer
BaDr Amer

Reputation: 910

Converting string to DateTime gives wrong result

I'm trying to convert a string to DateTime object like this:

DateTime.ParseExact("31/12/2017","dd/mm/yyyy",CultureInfo.InvariantCulture);

but I'm getting DateTime month number = 1 instead of 12 !!

Date = {1/31/2017 12:00:00 AM}

Upvotes: 1

Views: 898

Answers (1)

Nkosi
Nkosi

Reputation: 247018

The date format string is using minutes for the month. Use MM for month. mm is for minutes.

"mm" The minute, from 00 through 59.
"MM" The month, from 01 through 12.

Reference: Custom Date and Time Format Strings

Which would mean you need to update snippet to...

DateTime.ParseExact("31/12/2017","dd/MM/yyyy",CultureInfo.InvariantCulture);

Upvotes: 6

Related Questions