public static DateTime GetRelativeDate(int year, int month, RelativeDayInMonth relativeDay, DayOfWeek day) { var monthBegin = new DateTime(year, month, 1); var firstDay = GetNextWeekday(monthBegin, day); switch (relativeDay) { case RelativeDayInMonth.First: return(firstDay); case RelativeDayInMonth.Second: return(firstDay.AddDays(7)); case RelativeDayInMonth.Third: return(firstDay.AddDays(14)); case RelativeDayInMonth.Fourth: return(firstDay.AddDays(21)); case RelativeDayInMonth.Last: var totalDays = DateTime.DaysInMonth(year, month); return(firstDay.AddDays(Math.Floor((totalDays - firstDay.Day) / 7d) * 7)); default: throw new ArgumentOutOfRangeException(); } }
public void TestGetRelativeDate(int year, int month, RelativeDayInMonth relativeDay, DayOfWeek day, int expectedDay) { var result = ScheduledTriggerService.GetRelativeDate(year, month, relativeDay, day); Assert.Equal(year, result.Year); Assert.Equal(month, result.Month); Assert.Equal(expectedDay, result.Day); }