Get last/next week Wednesday date in C#

前端 未结 13 1647
情话喂你
情话喂你 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 19:52

    To find the next Wednesday just keep adding days until you find one. To find the previous Wednesday just keep subtracting days until you get to one.

    DateTime nextWednesday = DateTime.Now.AddDays(1);
    while (nextWednesday.DayOfWeek != DayOfWeek.Wednesday)
        nextWednesday = nextWednesday.AddDays(1);
    DateTime lastWednesday = DateTime.Now.AddDays(-1);
    while (lastWednesday.DayOfWeek != DayOfWeek.Wednesday)
        lastWednesday = lastWednesday.AddDays(-1);
    

提交回复
热议问题