Get last/next week Wednesday date in C#

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

    You'll want to use the DayOfWeek enum along with an if-else structure of switch statement to determine how many days to add/subtract for your dates. It's tedious coding but simple.

    DateTime nextRent;
    DateTime lastRent;
    DateTime today = DateTime.Now;
    
    if (today.DayOfWeek == DayOfWeek.Wednesday)
    {
       nextRent = today.AddDays(7);
       lastRent = today.AddDays(-7);
    }
    else if (today.DayOfWeek == DayOfWeek.Thursday)
    {
       nextRent = today.AddDays(6);
       lastRent = today.AddDays(-8);
    }
    //ect for all days
    

提交回复
热议问题