-
-
Notifications
You must be signed in to change notification settings - Fork 378
Next previous weekday
kappy edited this page Aug 30, 2011
·
1 revision
Some more general usable extensions on DateTime include the ability to get the next or previous DayOfWeek, or get the last or first DayOfWeek of the Month.
Example: Get next tuesday
[Test]
public void get_next_and_last_tuesday() {
var a_saturday = new DateTime(2011, 8, 20);
var nextTuesday = a_saturday.NextDayOfWeek(DayOfWeek.Tuesday);
var lastTuesday = a_saturday.LastDayOfWeek(DayOfWeek.Tuesday);
Assert.IsTrue((nextTuesday.DayOfWeek == DayOfWeek.Tuesday) && (nextTuesday == new DateTime(2011, 8, 23)));
Assert.IsTrue((lastTuesday.DayOfWeek == DayOfWeek.Tuesday) && (lastTuesday == new DateTime(2011, 8, 16)));
}
Example: First and Last Tuesday of August
[Test]
public void get_first_and_last_tuesday_of_august() {
var a_saturday = new DateTime(2011, 8, 13);
var firstTuesday = a_saturday.FirstDayOfWeekOfTheMonth(DayOfWeek.Tuesday);
var lastTuesday = a_saturday.LastDayOfWeekOfTheMonth(DayOfWeek.Tuesday);
Assert.IsTrue((firstTuesday.DayOfWeek == DayOfWeek.Tuesday) && (firstTuesday == new DateTime(2011, 8, 2)));
Assert.IsTrue((lastTuesday.DayOfWeek == DayOfWeek.Tuesday) && (lastTuesday == new DateTime(2011, 8, 30)));
}