Get last/next week Wednesday date in C#

前端 未结 13 1641
情话喂你
情话喂你 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:05

    Improved answer from Servy. You need to actually set the nextWednesday or lastWednesday dates to the new date in the while loop, otherwise it goes into an infinite loop

    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)
        lastWelastWednesday.AddDays(-1);
    

提交回复
热议问题