代码之家  ›  专栏  ›  技术社区  ›  Ryan

计算一个月的最后一天

  •  6
  • Ryan  · 技术社区  · 15 年前

    给定日期时间和dayOfWeek应返回该月最后一天的日期。

    例如。 2009年3月1日和周日将返回2009年3月29日

    9 回复  |  直到 6 年前
        1
  •  12
  •   Henk Holterman    9 年前

    找不到方便的一行程序,但这一行程序有效:

    static DateTime LastDayOfWeekInMonth(DateTime day, DayOfWeek dow)
    {
        DateTime lastDay = new DateTime(day.Year, day.Month, 1).AddMonths(1).AddDays(-1);
        DayOfWeek lastDow = lastDay.DayOfWeek;
    
        int diff = dow - lastDow;
    
        if (diff > 0) diff -= 7;
    
        System.Diagnostics.Debug.Assert(diff <= 0);
    
        return lastDay.AddDays(diff);
    }
    
        2
  •  11
  •   TFD    15 年前

    http://datetimeextensions.codeplex.com/ 有一个Last(DayOfWeek)扩展方法可以为您执行此操作

    测试内容:—)

        3
  •  6
  •   Guffa    15 年前

    得到下个月的第一天,然后找到那个月的第一个数学工作日。往回走七天,你在相应月份最后一周的工作日:

    DateTime date = new DateTime(2009, 3, 12);
    DayOfWeek day = DayOfWeek.Sunday;
    
    DateTime nextMonth = new DateTime(date.Year, date.Month, 1).AddMonths(1);
    while (nextMonth.DayOfWeek != day) {
        nextMonth = nextMonth.AddDays(1);
    }
    DateTime lastInMonth = nextMonth.AddDays(-7);
    

    (您可以用一些基于dayOfWeek值的数值计算要添加的天数的算法来替换循环,但这更直接。)

    编辑:
    当然,您也可以得到当前月份的最后一天,然后向后循环,直到找到正确的工作日。

        4
  •  4
  •       15 年前
    static DateTime LastDateOfWeekForMonth(DayOfWeek weekday, int month, int year)
    {
        DateTime d = new DateTime(year, month, 1).AddMonths(1);
        while (!(d.DayOfWeek == weekday && d.Month == month))
        {
           d = d.AddDays(-1);
        }
        return d;
    }
    
        5
  •  1
  •   Sorskoot    15 年前

    试着这样做:从最后一天开始倒数,直到你找到你想要的那一天。

    static DateTime LastOccurenceOfDay(DateTime dt, DayOfWeek dow)
        {
            DateTime looperDate = new DateTime(dt.Year, dt.Month, 1)
                                         .AddMonths(1).AddDays(-1);
            while (looperDate.DayOfWeek!=dow)                            
                looperDate =looperDate.AddDays(-1);            
            return looperDate;
    

    编辑: 在评论中引用了旧版本的恢复。

      static DateTime LastOccurenceOfDay(DateTime dt, DayOfWeek dow)
         {
             //set start of loop
             DateTime looperDate = new DateTime(dt.Year, dt.Month, 1); 
             //initialze return value
             DateTime returnDate = dt;
             //loop until the month is over
             while (looperDate.Month == dt.Month)
             {
                 //if the current DayOfWeek is the date you're looking for
                 if (looperDate.DayOfWeek == dow) 
                     //remember it.
                     returnDate = looperDate;
                 //increase day
                 looperDate=looperDate.AddDays(1);
             }
             return returnDate;
         }
    
        6
  •  1
  •   Andrei Sedoi    14 年前
        public DateTime GetLastDayOfMonth(int year, int month, DayOfWeek dayOfWeek)
        {
            var daysInMonth = DateTime.DaysInMonth(year, month);
            var lastDay = new DateTime(year, month, daysInMonth);
    
            while (lastDay.DayOfWeek != dayOfWeek)
            {
                lastDay = lastDay.AddDays(-1);
            }
    
            return lastDay;
        }
    
        7
  •  1
  •   Community noseratio    7 年前

    这里是lastdayofmonth()和lastdayofmonth(dayofweek)加上单元测试,它们都受到“last”的启发 星期五 “实施于 How to find the 3rd Friday in a month with C#? @ Paul Fryer:

    /**
        /// <summary>An extension method that returns the last day of the month.</summary>
        /// <param name="d">A date within the month to calculate.</param>
        /// <returns>The last day of the current month.</returns>
        **/
        public static DateTime LastDayOfMonth(this DateTime d)
        {
            DateTime nextMonth = new DateTime(d.Year, d.Month, 1).AddMonths(1);
            return nextMonth.AddDays(-1);
        }
    
        /**
        /// <summary>An extension method that returns the last <see cref="DayOfWeek"> of the month.</summary>
        /// <param name="d">A date within the month to calculate.</param>
        /// <returns>The last day of the current month.</returns>
        **/
        public static DateTime LastDayOfMonth(this DateTime d, DayOfWeek dayOfWeek)
        {
            DateTime lastDayOfMonth = d.LastDayOfMonth();
            int vector = (((int)lastDayOfMonth.DayOfWeek - (int)dayOfWeek + DaysInWeek) % DaysInWeek);
            return lastDayOfMonth.AddDays(-vector);
        }
    
     #region LastDayOfMonth Tests
    
        [TestCase("1/1/2011", "1/31/2011")]
        [TestCase("2/1/2009", "2/28/2009")] //Leap Year
        [TestCase("2/1/2008", "2/29/2008")] //Leap Year
        [TestCase("3/10/2011", "3/31/2011")]
        [TestCase("4/20/2011", "4/30/2011")]
        [TestCase("5/15/2011", "5/31/2011")]
        [TestCase("6/30/2011", "6/30/2011")]
        [TestCase("12/1/2011", "12/31/2011")]
        public void LastDayOfMonthReturnsCorrectDay(string startingDate, string expectedDate)
        {
            //Arrange
            DateTime testDate = DateTime.Parse(startingDate);
            DateTime expected = DateTime.Parse(expectedDate);
    
            //Act
            DateTime actual = testDate.LastDayOfMonth();
    
            //Assert
            Assert.That(actual, Is.EqualTo(expected));
        }
    
        [TestCase("1/1/2011", DayOfWeek.Monday, "1/31/2011")]
        [TestCase("2/1/2009", DayOfWeek.Saturday, "2/28/2009")]
        [TestCase("2/1/2009", DayOfWeek.Sunday, "2/22/2009")] 
        [TestCase("2/1/2008", DayOfWeek.Friday, "2/29/2008")] //Leap Year
        [TestCase("2/1/2008", DayOfWeek.Thursday, "2/28/2008")] //Leap Year
        [TestCase("3/10/2011", DayOfWeek.Wednesday, "3/30/2011")]
        [TestCase("4/20/2011", DayOfWeek.Friday, "4/29/2011")]
        [TestCase("4/20/2011", DayOfWeek.Saturday, "4/30/2011")]
        [TestCase("5/15/2011", DayOfWeek.Tuesday, "5/31/2011")]
        [TestCase("5/15/2011", DayOfWeek.Saturday, "5/28/2011")]
        [TestCase("9/30/2011", DayOfWeek.Sunday, "9/25/2011")]
        [TestCase("12/1/2011", DayOfWeek.Monday, "12/26/2011")]
        public void LastDayOfMonthReturnsCorrectDayOfWeek(string startingDate, DayOfWeek dayOfWeek, string expectedDate)
        {
            //Arrange
            DateTime testDate = DateTime.Parse(startingDate);
            DateTime expected = DateTime.Parse(expectedDate);
    
            //Act
            DateTime actual = testDate.LastDayOfMonth(dayOfWeek);
    
            //Assert
            Assert.That(actual, Is.EqualTo(expected));
        }       
    
        #endregion
    
        8
  •  0
  •   sth Alien    14 年前
    static DateTime GetDayOfWeekInMonthFromWeekIndex(this DateTime date, DayOfWeek dow, int weekIndex)
    {
        if (weekIndex > 4)
            return LastDayOfWeekInMonth(date, dow);
    
        DateTime newDate = new DateTime(date.Year, date.Month, 1);
        DayOfWeek newDow = newDate.DayOfWeek;
        int diff = dow - newDow;
        diff += ((weekIndex - 1) * 7);
        return newDate.AddDays(diff);
    }
    
        9
  •  0
  •   dwneder    6 年前

    它提到:

    int lastdayofmonth=datetime.daysinmonth(now.year,now.month);