Next higher month day
This snippet describes how to find the next X day after a given Date time.
<br/>
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);
}
//Example:
// 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
Url: http://www.jonasjohn.de/snippets/csharp/next-higher-month-day.htm
Language: C# | User: ShareMySnippets | Created: Oct 16, 2013