【问题标题】:How to find the next august? C#如何找到下一个八月? C#
【发布时间】:2009-05-11 14:19:50
【问题描述】:

我有以下问题:

我们需要找到下一个八月。换句话说,如果我们是 2009-09-01,我们需要 2010-08-31,如果我们是 2009-06-21,我们需要 2009-08-31。

我知道我可以检查今天是否小于 8 月 31 日,但我想知道是否还有其他可能性。

【问题讨论】:

  • 接下来我们要问的是如何增加一个int...

标签: c# datetime


【解决方案1】:
    public static class DateTimeExtensions
    {
        public static DateTime GetNextAugust31(this DateTime date)
        {
            return new DateTime(date.Month <= 8 ? date.Year : date.Year + 1, 8, 31);
        }
    }

【讨论】:

    【解决方案2】:

    .Net 2.0

        DateTime NextAugust(DateTime inputDate)
        {
            if (inputDate.Month <= 8)
            {
                return new DateTime(inputDate.Year, 8, 31);
            }
            else
            {
                return new DateTime(inputDate.Year+1, 8, 31);
            }
        }
    

    【讨论】:

    • 这实际上是不正确的。您将返回 inputDate.Month,它不会是八月。这将是发送到该方法的月份。
    • 它有 12 分之一的工作机会 :) 现在全年为 0.2
    【解决方案3】:
    public static DateTime NextAugust(DateTime input)
    {
        switch(input.Month.CompareTo(8))
        {
            case -1:
            case 0: return new DateTime(input.Year, 8, 31);
            case 1:
                return new DateTime(input.Year + 1, 8, 31);
            default:
                throw new ApplicationException("This should never happen");
        }
    }
    

    【讨论】:

      【解决方案4】:

      这行得通。一定要添加一些异常处理。例如,如果您为 feb 传入了 31,则会引发异常。

      /// <summary>
              /// Returns a date for the next occurance of a given month
              /// </summary>
              /// <param name="date">The starting date</param>
              /// <param name="month">The month requested. Valid values (1-12)</param>
              /// <param name="day">The day requestd. Valid values (1-31)</param>
              /// <returns>The next occurance of the date.</returns>
              public DateTime GetNextMonthByIndex(DateTime date, int month, int day)
              {
                  // we are in the target month and the day is less than the target date
                  if (date.Month == month && date.Day <= day)
                      return new DateTime(date.Year, month, day);
      
                  //add month to date until we hit our month
                  while (true)
                  {
                      date = date.AddMonths(1);
                      if (date.Month == month)
                          return new DateTime(date.Year, month, day);
                  }
              }
      
              private void Form1_Load(object sender, EventArgs e)
              {
                  DateTime d = DateTime.Now;            
      
                  //get the next august
                  Text = GetNextMonthByIndex(d, 8, 31).ToString();
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-20
        • 1970-01-01
        • 1970-01-01
        • 2011-05-03
        • 2023-03-13
        • 1970-01-01
        • 2017-02-22
        • 1970-01-01
        相关资源
        最近更新 更多