Dates.Recurring icon indicating copy to clipboard operation
Dates.Recurring copied to clipboard

Calculation of the last day if it matches the endDate

Open Reaper922 opened this issue 11 months ago • 0 comments

Hey :)

I just noticed that the calculation of the last day differs for each period when the last day matches the endDate. For the daily/weekly period, the last day is included in the calculation if it is the same as the endDate. However, for the monthly/yearly period, it is not included. Is there a reason for this difference, or is it a bug?

In the following example I used the weekly calculation and the endDate is the 29.01.2024 and the last calculated date (Monday) is also the 29.01.2024. In the second example I used the monthly calculation and the last calculated day also matches the endDate but is not included in the result.

I would expect the endDate to always be included in the result or at least the same behaviour for all periods. Appreciate your feedback.

// First example
var dates = new List<DateTime>();

var startDate = new DateTime(2024, 1, 1);
var endDate = new DateTime(2024, 1, 29);

var weekly = Recurs
  .Starting(startDate)
  .Every(1)
  .Weeks()
  .OnDay(DayOfWeek.Monday)
  .Ending(endDate)
  .Build();

DateTime? nextDate = DateTime.MinValue;

while (nextDate.HasValue && nextDate <= endDate)
{
  if (nextDate != DateTime.MinValue) dates.Add(nextDate.Value);
  nextDate = weekly.Next(nextDate.Value);
}


// Second example
var dates = new List<DateTime>();

var startDate = new DateTime(2024, 1, 1);
var endDate = new DateTime(2024, 3, 1);

var monthly = Recurs
  .Starting(startDate)
  .Every(1)
  .Months()
  .OnDay(1)
  .Ending(endDate)
  .Build();

DateTime? nextDate = DateTime.MinValue;

while (nextDate.HasValue && nextDate <= endDate)
{
  if (nextDate != DateTime.MinValue) dates.Add(nextDate.Value);
  nextDate = monthly.Next(nextDate.Value);
}

Reaper922 avatar Mar 14 '24 15:03 Reaper922