How would I get last week Wednesday and next week Wednesday\'s date in C#:
public Form1()
{
InitializeComponent();
CurrentDate.Text = \"Today\'s Date: \" +
You could create 2 DateTime Extension methods, that can be used with a DayOfWeek parameter:
public static class DateTimeExtension
{
public static DateTime GetPreviousWeekDay(this DateTime currentDate, DayOfWeek dow)
{
int currentDay = (int)currentDate.DayOfWeek, gotoDay = (int)dow;
return currentDate.AddDays(-7).AddDays(gotoDay-currentDay);
}
public static DateTime GetNextWeekDay(this DateTime currentDate, DayOfWeek dow)
{
int currentDay = (int)currentDate.DayOfWeek, gotoDay = (int)dow;
return currentDate.AddDays(7).AddDays(gotoDay - currentDay);
}
}
Which can then be used as follows:
DateTime testDate = new DateTime(2017, 01, 21);
Console.WriteLine(testDate.GetPreviousWeekDay(DayOfWeek.Wednesday));
Console.WriteLine(testDate.GetNextWeekDay(DayOfWeek.Wednesday));