If given a date and a variable n, how can I calculate the DateTime for which the day of the month will be the nth Date?
For example, Today is the 17th of June. I wou
After many, many edits, corrections and re-writes, here is my final answer:
The method that follows returns a a DateTime
representing the next time the day of number day
comes up in the calendar. It does so using an iterative approach, and is written in the form of an extension method for DateTime
objects, and thus isn't bound to today's date but will work with any date.
The code executes the following steps to get the desired result:
cDate
's month works (the day must not have passed, and the month must have enough days in it).
includeToday
to true so that the first day of the new month is included, and execute the loop again.The code:
static DateTime GetNextDate3(this DateTime cDate, int day, bool includeToday = false)
{
// Make sure provided day is valid
if (day > 0 && day <= 31)
{
while (true)
{
// See if day has passed in current month or is not contained in it at all
if ((includeToday && day > cDate.Day || (includeToday && day >= cDate.Day)) && day <= DateTime.DaysInMonth(cDate.Year, cDate.Month))
{
// If so, break and return
break;
}
// Advance month by one and set day to one
// FIXED BUG HERE (note the order of the two calls)
cDate = cDate.AddDays(1 - cDate.Day).AddMonths(1);
// Set includeToday to true so that the first of every month is taken into account
includeToday = true;
}
// Return if the cDate's month contains day and it hasn't passed
return new DateTime(cDate.Year, cDate.Month, day);
}
// Day provided wasn't a valid one
throw new ArgumentOutOfRangeException("day", "Day isn't valid");
}