Get last/next week Wednesday date in C#

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

    You can use this to calculate it:

    DateTime day = DateTime.Today;
    while (day.DayOfWeek != DayOfWeek.Wednesday)
        day = day.AddDays(-1);
    var currentRent = day;
    var nextRent = day.AddDays(7);
    

    Note that if today is Wednesday, this will show currentRent as today, not nextRent as today. If you want this reversed, you can reverse the logic.

    DateTime day = DateTime.Today;
    while (day.DayOfWeek != DayOfWeek.Wednesday)
        day = day.AddDays(1);
    var currentRent = day.AddDays(-7);
    var nextRent = day;
    

提交回复
热议问题