Mark
Mark

Reputation: 3777

C# DateTime.AddMonth with day non-existent in next month

If I have DateTime date = new DateTime("1/31/2010"); and I call date.AddMonth(1). Will I get "2/28/2010" or will it freak out because "2/31/2010" doesn't exist? Or will I get "3/3/2010"?

(Also I'm not near a computer with Visual Studio)

Upvotes: 14

Views: 11672

Answers (4)

JohnB
JohnB

Reputation: 19012

If the resulting day is not a valid day in the resulting month, the last valid day of the resulting month is used. For example, March 31st + 1 month = April 30th.

Upvotes: 53

Adam
Adam

Reputation: 3013

It will not break, it will give you 2/28/2010

Upvotes: 4

Sergi Papaseit
Sergi Papaseit

Reputation: 16204

It will do a proper DateTime month addition and return 2/28/2010

Upvotes: 4

Chris Dixon
Chris Dixon

Reputation: 9167

You'll get the end day of the next month, so 2/28.

Upvotes: 7

Related Questions