How would I get last week Wednesday and next week Wednesday\'s date in C#:
public Form1()
{
InitializeComponent();
CurrentDate.Text = \"Today\'s Date: \" +
Here is a one-liner to accomplish the same. It is somewhat mind-boggling to understand how that actually works but it does:
Getting next Wednesday:
dt.AddDays(-(int)(dt.AddDays(-4).DayOfWeek) + 6);
Getting last Wednesday
dt.AddDays(-(int)(dt.AddDays(-3).DayOfWeek));
In both cases, it will return the day itself when it is Wednesday. This works for any weekdays, just adjust the number in the AddDays() call. For example for Friday:
Getting next Friday
dt.AddDays(-(int)(dt.AddDays(-6).DayOfWeek) + 6);
Getting last Friday
dt.AddDays(-(int)(dt.AddDays(-5).DayOfWeek));