Tanmoy
Tanmoy

Reputation: 45652

DateTime ParseExact conversion increasing month by 1

Trying to use ParseExact to convert a string to datetime but the resulted datetime seems to be increasing the month by 1. What am I missing

DateTime.ParseExact("7/22/2015 8:08:01 PM", "m/d/yyyy h:M:s tt", CultureInfo.InvariantCulture)

Result: 22-08-2015 20:07:01

Upvotes: 1

Views: 209

Answers (3)

LInsoDeTeh
LInsoDeTeh

Reputation: 1038

You mixed up m and M for minutes and months. So it's just coincidence it looks like the month is increased by 1.

The correct code would be:

DateTime.ParseExact("7/22/2015 8:08:01 PM", "M/d/yyyy h:m:s tt", CultureInfo.InvariantCulture)

Upvotes: 5

tolanj
tolanj

Reputation: 3724

m is minute, M is month. The code is not increasing the month by one but picking out the 08 minute part of the input. You want

"M/d/yyyy h:m:s tt"

Upvotes: 3

Fabjan
Fabjan

Reputation: 13676

Lol, change it to :

var d = DateTime.ParseExact("7/22/2015 8:08:01 PM", "M/d/yyyy h:m:s tt", CultureInfo.InvariantCulture);

Upvotes: 4

Related Questions