【问题标题】:How do I get the last day of a month?我如何获得一个月的最后一天?
【发布时间】:2011-01-30 09:14:31
【问题描述】:

如何在 C# 中找到一个月的最后一天?

例如,如果我的日期是 03/08/1980,如何获得第 8 个月的最后一天(在本例中为 31)?

【问题讨论】:

  • @Mark:我能问什么?我认为您自己的答案不需要扩展方法。
  • 最后一天不仅仅特定于月份,还需要年份。 2010 年 2 月的最后一天是 28 日,但 2008 年 2 月的最后一天是 29 日。
  • @abatishchev 它不需要扩展方法,但问题并没有真正要求它。但是,至少对我来说,看到它会更好,更具可读性。扩展方法更多的是一种建议。任何解决方案都可以在扩展方法中工作,而不仅仅是我的。

标签: c# .net datetime


【解决方案1】:

你得到这样的一个月的最后一天,它返回 31:

DateTime.DaysInMonth(1980, 08);

【讨论】:

  • public static DateTime ConvertToLastDayOfMonth(DateTime date) { return new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month)); } 以日期格式获取该月的最后一天
  • 超级好用:-)
  • 或“new DateTime(1980,8,3).AddMonths(1).AddDays(-3);”。 (-3) 是天数,所以下个月的 0 基本上是当月的最后一天。
【解决方案2】:
var lastDayOfMonth = DateTime.DaysInMonth(date.Year, date.Month);

【讨论】:

  • @Henk 实际上,我从源代码中的某个位置提取了这个,该位置从lastDayOfMonth 创建了DateTime。老实说,无论哪种方式都非常有效。这是一个迂腐的论点,哪种方式更好。我两种方法都做了,都得到了相同的答案。
【解决方案3】:

如果你想要 日期,给定一个月和一年,这似乎是正确的:

public static DateTime GetLastDayOfMonth(this DateTime dateTime)
{
    return new DateTime(dateTime.Year, dateTime.Month, DateTime.DaysInMonth(dateTime.Year, dateTime.Month));
}

【讨论】:

    【解决方案4】:

    从下个月的第一天减去一天:

    DateTime lastDay = new DateTime(MyDate.Year,MyDate.Month+1,1).AddDays(-1);
    

    另外,如果您也需要它在 12 月工作:

    DateTime lastDay = new DateTime(MyDate.Year,MyDate.Month,1).AddMonths(1).AddDays(-1);
    

    【讨论】:

    • 你的第二行代码很好,你应该删除第一行以避免混淆。
    【解决方案5】:

    您可以通过此代码找到任何月份的最后日期:

    var now = DateTime.Now;
    var startOfMonth = new DateTime(now.Year, now.Month, 1);
    var DaysInMonth = DateTime.DaysInMonth(now.Year, now.Month);
    var lastDay = new DateTime(now.Year, now.Month, DaysInMonth);
    

    【讨论】:

      【解决方案6】:
      // Use any date you want, for the purpose of this example we use 1980-08-03.
      var myDate = new DateTime(1980,8,3);
      var lastDayOfMonth = new DateTime(myDate.Year, myDate.Month, DateTime.DaysInMonth(myDate.Year, myDate.Month));
      

      【讨论】:

      • 此答案仅响应提问者的示例。更全面的答案会更有帮助。
      • 您可以使用任何您想要的日期作为输入值,因此您可以将此解决方案应用于您想要的任何日期。
      • 某些日期会失败,请尝试var myDate = new DateTime(1980, 1, 31);(返回 29 日)
      • Hans Kesting,你是对的。如果下个月的天数少于当前天数,此方法将失败。我想最简单的方法是使用 DateTime.DaysInMonth() 但这是公认的答案,所以我的答案应该被删除。
      【解决方案7】:

      你可以通过一行代码找到一个月的最后一天:

      int maxdt = (new DateTime(dtfrom.Year, dtfrom.Month, 1).AddMonths(1).AddDays(-1)).Day;
      

      【讨论】:

      • 我很奇怪,不是简单的方法:DateTime.DaysInMonth,为什么有人要寻找这种不可读且复杂的方法来实现它!? - 但作为一个有效的解决方案是可以接受的;)。
      【解决方案8】:

      来自DateTimePicker:

      第一次约会:

      DateTime first_date = new DateTime(DateTimePicker.Value.Year, DateTimePicker.Value.Month, 1);
      

      最后日期:

      DateTime last_date = new DateTime(DateTimePicker.Value.Year, DateTimePicker.Value.Month, DateTime.DaysInMonth(DateTimePicker.Value.Year, DateTimePicker.Value.Month));
      

      【讨论】:

        【解决方案9】:

        您可以按如下方式扩展 DateTime;

        public static class DateTimeMethods
            {
                public static DateTime StartOfMonth(this DateTime date)
                {
                    return new DateTime(date.Year, date.Month, 1, 0, 0, 0);
                }
        
                public static DateTime EndOfMonth(this DateTime date)
                {
                    return date.StartOfMonth().AddMonths(1).AddSeconds(-1);
                }
            }
        

        并按如下方式使用;

        DateTime today = DateTime.Now;
        
        DateTime startOfMonth = today.StartOfMonth();
        DateTime endOfMonth = today.EndOfMonth();
        

        【讨论】:

          【解决方案10】:

          在特定日历中获取一个月的最后一天 - 并在扩展方法中 - :

          public static int DaysInMonthBy(this DateTime src, Calendar calendar)
          {
              var year = calendar.GetYear(src);                   // year of src in your calendar
              var month = calendar.GetMonth(src);                 // month of src in your calendar
              var lastDay = calendar.GetDaysInMonth(year, month); // days in month means last day of that month in your calendar
              return lastDay;
          }
          

          【讨论】:

            【解决方案11】:

            这将显示下个月的最后一个日期。您可以通过从 AddMonths(x) 中添加或减去它来添加要返回的一年中的月份

            DateTime.Now.AddMonths(2).AddDays(-DateTime.Now.Day)
            

            【讨论】:

              【解决方案12】:

              2021 年 5 月 7 日示例

              DateTime.Now.Day;

              结果:5

              DateTime.Now.AddMonths(+1).AddDays(-DateTime.Now.Day).ToString("yyyy-MM-dd");

              结果:2021/07/31

              【讨论】:

                【解决方案13】:

                这个公式反映了@RHSeeger 的想法,它是获取(在本例中)第 3 个月的最后一天(单元格 A1 + 4 中的日期月份,该月的第一天减去 1 天)的简单解决方案:

                =DATE(YEAR(A1);MONTH(A1)+4;1)-1
                

                非常精确,包括闰年的二月:)

                【讨论】:

                  【解决方案14】:

                  另一种获取结束日期的方法:

                      private static DateTime GetMonthEndDate(DateTime date)
                      {
                          DateTime endDate = date;
                          int endDateMonth = endDate.Month;
                  
                          while (endDateMonth == endDate.Month)
                              endDate = endDate.AddDays(1);
                  
                          endDate = endDate.AddDays(-1);
                  
                          return endDate;
                      }
                  

                  【讨论】:

                    【解决方案15】:

                    我只想在每个月的最后一天触发代码,就这么简单......

                    if (DateTime.UtcNow.AddDays(1).Day != 1)
                    {
                        // Tomorrow is not the first...
                        return;
                    }
                    

                    【讨论】:

                      【解决方案16】:

                      我不懂 C#,但如果发现没有方便的 API 方法来获取它,那么您可以这样做的一种方法是遵循以下逻辑:

                      today -> +1 month -> set day of month to 1 -> -1 day
                      

                      当然,这假设你有这种类型的日期数学。

                      【讨论】:

                      • 这不准确。
                      • 实际上在许多语言中获取一个月的最后一天是准确的逻辑,包括 c# "new DateTime(1980,8,1).AddMonths(1).AddDays(-1);"像这样基本。
                      猜你喜欢
                      • 2010-09-07
                      • 1970-01-01
                      • 2015-03-26
                      • 1970-01-01
                      • 2012-04-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多