How would I get last week Wednesday and next week Wednesday\'s date in C#:
public Form1()
{
InitializeComponent();
CurrentDate.Text = \"Today\'s Date: \" +
this extension method should do the trick for whatever dayofweek
public static class DateTimeExtensions
{
public static DateTime LastDayOfWeek(this DateTime _date, DayOfWeek dayofweek)
{
return _date.AddDays(-1 * ((_date.DayOfWeek - dayofweek) % 7)).Date;
}
public static DateTime NextDayOfWeek(this DateTime _date, DayOfWeek dayofweek)
{
return _date.LastDayOfWeek(dayofweek).AddDays(7).Date;
}
}
usage
var lastWendsday = DateTime.Now.LastDayOfWeek(DayOfWeek.Wednesday);