Get last/next week Wednesday date in C#

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

    One of the problems with some of the answers is that the DayoyOfWeek is an enum with values 0-6. When a subsequent day of week equates to Sunday (enum value = 0), but you gave a Tuesday (enum value=2), and you want next tuesday, the calculation goes wrong if you just peform a calculation.

    The class with two methods which I cam up with for my own use on a project I am working on is.

    public static class DateHelper
    {
        public static DateTime GetDateForLastDayOfWeek(DayOfWeek DOW, DateTime DATE)
        {
            int adjustment = ((int)DATE.DayOfWeek < (int)DOW ? 7 : 0);
            return DATE.AddDays(0- (((int)(DATE.DayOfWeek) + adjustment) - (int)DOW));
        }
    
        public static DateTime GetDateForNextDayOfWeek(DayOfWeek DOW, DateTime DATE)
        {
            int adjustment = ((int)DATE.DayOfWeek < (int)DOW ? 0 : 7);
            return DATE.AddDays(((int)DOW) - ((int)(DATE.DayOfWeek)) + adjustment);
        }
    }
    

    The XUnit tests proving the code above works.

    public class DateHelperUnitTests
    {
    
        [Theory]
        [InlineData(2020, 1, 7, 2020, 1, 7)]
        [InlineData(2020, 1, 8,  2020, 1, 7)]
        [InlineData(2020, 1, 9,  2020, 1, 7)]
        [InlineData(2020, 1, 10, 2020, 1, 7)]
        [InlineData(2020, 1, 11, 2020, 1, 7)]
        [InlineData(2020, 1, 12, 2020, 1, 7)]
        [InlineData(2020, 1, 13, 2020, 1, 7)]
        [InlineData(2020, 1, 14, 2020, 1, 14)]
        [InlineData(2020, 1, 15, 2020, 1, 14)]
        public void GetDateForLastDayOfWeek_MultipleValues_Pass(
            int InputYear, int InputMonth, int InputDay,
            int ExpectedYear, int ExpectedMonth, int ExpectedDay)
        {
            DateTime DateToTest = new DateTime(InputYear, InputMonth, InputDay);
            DateTime NewDate = DateHelper.GetDateForLastDayOfWeek(DayOfWeek.Tuesday, DateToTest);
            DateTime DateExpected = new DateTime(ExpectedYear,ExpectedMonth,ExpectedDay);
    
            Assert.True(0 == DateTime.Compare(DateExpected.Date, NewDate.Date));
        }
    
        [Theory]
        [InlineData(2020, 1, 7, 2020, 1, 14)]
        [InlineData(2020, 1, 8, 2020, 1, 14)]
        [InlineData(2020, 1, 9, 2020, 1, 14)]
        [InlineData(2020, 1, 10, 2020, 1, 14)]
        [InlineData(2020, 1, 11, 2020, 1, 14)]
        [InlineData(2020, 1, 12, 2020, 1, 14)]
        [InlineData(2020, 1, 13, 2020, 1, 14)]
        [InlineData(2020, 1, 14, 2020, 1, 21)]
        [InlineData(2020, 1, 15, 2020, 1, 21)]
        public void GetDateForNextDayOfWeek_MultipleValues_Pass(
            int InputYear, int InputMonth, int InputDay,
            int ExpectedYear, int ExpectedMonth, int ExpectedDay)
        {
            DateTime DateToTest = new DateTime(InputYear, InputMonth, InputDay);
            DateTime NewDate = DateHelper.GetDateForNextDayOfWeek(DayOfWeek.Tuesday, DateToTest);
            DateTime DateExpected = new DateTime(ExpectedYear, ExpectedMonth, ExpectedDay);
    
            Assert.True(0 == DateTime.Compare(DateExpected.Date, NewDate.Date));
        }
    }
    

提交回复
热议问题