public void TestDateTimeAddDaysUntilPrevDayOfWeekEquals() { DateTime dateTime = new DateTime(2010, 12, 2); DateTime ignore = dateTime.AddDaysUntilPrevDayOfWeek(DayOfWeek.Thursday, false); DateTime add = dateTime.AddDaysUntilPrevDayOfWeek(DayOfWeek.Thursday, true); Assert.IsTrue( (ignore.Year == 2010) && (ignore.Month == 12) && (ignore.Day == 2) && (dateTime.DayOfWeek == DayOfWeek.Thursday)); Assert.IsTrue( (add.Year == 2010) && (add.Month == 11) && (add.Day == 25) && (add.DayOfWeek == DayOfWeek.Thursday)); }
public void TestDateTimeAddDaysUntilPrevDayOfWeek() { DateTime dateTime = new DateTime(2010, 12, 2); dateTime = dateTime.AddDaysUntilPrevDayOfWeek(DayOfWeek.Sunday); Assert.IsTrue( (dateTime.Year == 2010) && (dateTime.Month == 11) && (dateTime.Day == 28) && (dateTime.DayOfWeek == DayOfWeek.Sunday)); }
protected override IList <DateTime> GetDatesForWeeks(DateTime start, DateTime end) { List <DateTime> res = new List <DateTime>(); DateTime date = DateTime.Now; if (Year == ANYYEAR) //All years are integrated { //Create the first date from start with condition switch (CounterType) { case CounterType.Front: date = new DateTime(start.Year, 1, 1); date = date.AddDaysUntilNextDayOfWeek(this.DayOfWeek); date = date.AddDays(Value * 7); break; case CounterType.Back: date = new DateTime(start.Year, 12, 31); date = date.AddDaysUntilPrevDayOfWeek(this.DayOfWeek); date = date.AddDays(-(Value * 7)); break; default: throw new NotImplementedException(); } if (date < start) //Date is to small at start { date.AddYears(1); //Add year to go next step } else if (date > end) //Date is to great at start { return(null); //Stop routine, no date found } //Search all dates in one year steps while ((date >= start) && (date <= end)) { res.Add(date); date.AddYears(1); } } else //Fixed year are integrated { //Create the fixed date from condition switch (CounterType) { case CounterType.Front: date = new DateTime(this.Year, 1, 1); date = date.AddDaysUntilNextDayOfWeek(this.DayOfWeek); date = date.AddDays(Value * 7); break; case CounterType.Back: date = new DateTime(this.Year, 12, 31); date = date.AddDaysUntilPrevDayOfWeek(this.DayOfWeek); date = date.AddDays(-(Value * 7)); break; default: throw new NotImplementedException(); } //If date between start and end date if ((date >= start) && (date <= end)) { res.Add(date); //Add it } } if (res.Count > 0) { return(res); } return(null); }