Next higher month day

This snippet describes how to find the next X day after a given Date time.
That can be very useful, for example, if you want to find the next 15th day or so after a specific date.

/// <summary>
/// Returns the next month day after the given DateTime.
/// </summary>
/// <param name="T">Source DateTime</param>
/// <param name="D">Target month day</param>
/// <returns>DateTime</returns>
public DateTime NextHigherMonthDay(DateTime T, int D) {
    return ((T.Day >= D) ? T.AddMonths(1) : T).AddDays(D-T.Day);
// With a minor change, you can create related functions like
// the one below. I just changed the ">=" to ">" ...
public DateTime NextHigherOrEqualMonthDay(DateTime T, int D)
    return ((T.Day > D) ? T.AddMonths(1) : T).AddDays(D - T.Day);
Snippet Details

// Testdate (August 10, 2007):
DateTime TestDate = new DateTime(2007, 8, 10, 20, 15, 0);
// Test 1:
DateTime NewDate1 = NextHigherMonthDay(TestDate, 10);
// The will be: September 10, 2007 because it is
// the next 10th day after the given date
// Test 2:
DateTime NewDate2 = NextHigherMonthDay(TestDate, 11);
// The will be: August 11, 2007 because it is
// the next 11th day after the given date
// Test 3:
DateTime NewDate3 = NextHigherMonthDay(TestDate, 3);
// The will be: September 3, 2007 because it is
// the next 3rd day after the given date

Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!

Older comments:

Rob March 04, 2011 at 01:12
It produces unexpected results if you're looking for day 31.