Get last/next week Wednesday date in C#

前端 未结 13 1683
情话喂你
情话喂你 2021-02-18 19:50

How would I get last week Wednesday and next week Wednesday\'s date in C#:

public Form1()
{
   InitializeComponent();
   CurrentDate.Text = \"Today\'s Date: \" +         


        
13条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-18 20:02

    DateTime.Now.AddDays(7) and DateTime.Now.AddDays(-7) is how you can do arithmetic, assuming you are on Wednesday. If you aren't, what you would need to do is use the DayOfWeek property to determine the number of days (positive and negative) that you would need to determine which day is 'Wednesday'. Then you can pass that value into AddDays.

    For instance, if today was tuesday, you would AddDays(-6) for last Wednesday and AddDays(8) for next Wednesday.

    I'll leave you the task of calculating those.

提交回复
热议问题