Get last/next week Wednesday date in C#

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

        private static DateTime FindPreviousDayOfWeek(DateTime fromDate, DayOfWeek findDay, 
            bool skipSame = false)
        {
            if (fromDate.DayOfWeek < findDay)
                fromDate = fromDate.AddDays(-((int)fromDate.DayOfWeek - 1 + (int)findDay));
            else if (fromDate.DayOfWeek > findDay)
                fromDate = fromDate.AddDays(-((int)fromDate.DayOfWeek - (int)findDay));
            else if (fromDate.DayOfWeek == findDay && skipSame == true)
                fromDate = fromDate.AddDays(-7);
    
            return fromDate;
        }
    

    the skipSame variable does not include the current day if it is the saem as the desired day.

提交回复
热议问题