public void TestDateTimeAddDaysUntilNextDayOfWeekEquals() { DateTime dateTime = new DateTime(2010, 12, 2); DateTime ignore = dateTime.AddDaysUntilNextDayOfWeek(DayOfWeek.Thursday, false); DateTime add = dateTime.AddDaysUntilNextDayOfWeek(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 == 12) && (add.Day == 9) && (add.DayOfWeek == DayOfWeek.Thursday)); }
public void TestDateTimeAddDaysUntilNextDayOfWeek() { DateTime dateTime = new DateTime(2010, 12, 2); dateTime = dateTime.AddDaysUntilNextDayOfWeek(DayOfWeek.Sunday); Assert.IsTrue( (dateTime.Year == 2010) && (dateTime.Month == 12) && (dateTime.Day == 5) && (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); }