Get last/next week Wednesday date in C#

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

    You could create 2 DateTime Extension methods, that can be used with a DayOfWeek parameter:

    public static class DateTimeExtension
    {
        public static DateTime GetPreviousWeekDay(this DateTime currentDate, DayOfWeek dow)
        {
            int currentDay = (int)currentDate.DayOfWeek, gotoDay = (int)dow;
            return currentDate.AddDays(-7).AddDays(gotoDay-currentDay);
        }
    
        public static DateTime GetNextWeekDay(this DateTime currentDate, DayOfWeek dow)
        {
            int currentDay = (int)currentDate.DayOfWeek, gotoDay = (int)dow;
            return currentDate.AddDays(7).AddDays(gotoDay - currentDay);
        }
    }
    

    Which can then be used as follows:

    DateTime testDate = new DateTime(2017, 01, 21);
    
    Console.WriteLine(testDate.GetPreviousWeekDay(DayOfWeek.Wednesday));
    Console.WriteLine(testDate.GetNextWeekDay(DayOfWeek.Wednesday));
    
    0 讨论(0)
  • 2021-02-18 20:05

    This will work. You need to calculate the difference in days between your provided date and the nearest Wednesday, and calculate last/next Wednesday based on whether or not the difference is greater than zero.

    int difference = date.DayOfWeek - DayOfWeek.Wednesday;
    DateTime lastWednesday = difference > 0 ? date.AddDays(-1 * difference) : date.AddDays(-1 * (7 + difference));
    DateTime nextWednesday = lastWednesday.AddDays(7);
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-02-18 20:15

    this extension method should do the trick for whatever dayofweek

       public static class DateTimeExtensions
        {
            public static DateTime LastDayOfWeek(this DateTime _date, DayOfWeek dayofweek)
            {
                return _date.AddDays(-1 * ((_date.DayOfWeek - dayofweek) % 7)).Date;
            }
    
            public static DateTime NextDayOfWeek(this DateTime _date, DayOfWeek dayofweek)
            {
                return _date.LastDayOfWeek(dayofweek).AddDays(7).Date;
            }
        }
    

    usage

    var lastWendsday = DateTime.Now.LastDayOfWeek(DayOfWeek.Wednesday);
    
    0 讨论(0)
  • 2021-02-18 20:16

    Use the AddDays routine:

            // increment by the number of offset days to get the correct date
            DayOfWeek desiredDay = DayOfWeek.Wednesday;
            int offsetAmount = (int) desiredDay - (int) DateTime.Now.DayOfWeek;
            DateTime lastWeekWednesday = DateTime.Now.AddDays(-7 + offsetAmount);
            DateTime nextWeekWednesday = DateTime.Now.AddDays(7 + offsetAmount);
    

    That should do it!

    NOTE: If it is a Monday, "Last Wednesday" is going to give you the very last Wednesday that occurred, but "Next Wednesday" is going to give you the Wednesday 9 days from now! If you wanted to get the Wednesday in two days instead you would need to use the "%" operator. That means the second "nextweek" statement would read "(7 + offsetAmount) % 7".

    0 讨论(0)
提交回复
热议问题