【问题标题】:Calculate the number of Calendar Months between two dates [duplicate]计算两个日期之间的日历月数[重复]
【发布时间】:2012-12-26 18:42:45
【问题描述】:

我正在用 C# 开发一个兴趣计算器。我需要知道 2 dates 之间的天数

所以,我正在计算这样的东西:

    DateTime dt1 = DateTime.ParseExact(DateTime.Now.Date.ToString("dd-MM-yy"), "dd-MM-yy", CultureInfo.InvariantCulture).Date;
    DateTime dt2 = DateTime.ParseExact(Duration, "dd-MM-yy", CultureInfo.InvariantCulture).Date;

    Double no_days = (dt1 - dt2).TotalDays;

但是,根据月份的不同,天数会有所不同。因此,2 月 15 日到 3 月 15 日是一个月,即使天数少于 30。

知道如何确定经过的月数吗?利息计算在月末进行。

谢谢

【问题讨论】:

标签: c# date datetime timespan


【解决方案1】:

我想不出比这更干净的方法了:

((dt1.Year - dt2.Year) * 12) + (dt1.Month - dt2.Month) - (dt1.Day < dt2.Day?1:0);

即便如此,我也不确定我是否错过了一些边缘情况。

【讨论】:

  • +1 你是个聪明人。这只是替换了 12 行相当复杂的代码。
【解决方案2】:

我最终编写了自己的例程。我希望它会帮助别人。如果有更简单的方法来计算两个日期之间的月份,请告诉我。

DateTime dt1 = DateTime.ParseExact(DateTime.Now.Date.ToString("dd-MM-yy"), "dd-MM-yy", CultureInfo.InvariantCulture).Date;
DateTime dt2 = DateTime.ParseExact(Duration, "dd-MM-yy", CultureInfo.InvariantCulture).Date;

DateTime temp = dt2;
int no_months = 0;
while ((dt1 - temp).TotalDays > 0)
{
    temp = temp.AddMonths(1);
    no_months++;
}

if (no_months > 0)
{
    no_months = no_months - 1;
}

谢谢

【讨论】:

    【解决方案3】:
    int MonthsElapsed = ((DateB.AddDays(1).Year - DateA.AddDays(1).Year) * 12) +
                        (DateB.AddDays(1).Month - DateA.AddDays(1).Month) -
                        (DateB.AddDays(1).Day < DateA.AddDays(1).Day ? 1 : 0);
    

    这个公式做了一些假设:

    1. 整月是 DateA 的日期和未来月份的同一天之间的跨度。 (例如,1/15-2/15 = 1 个月,1/15-3/15 = 2 个月等)我们将其称为触发日期。
    2. 任何月份中不包含触发日期的最后一天都相当于触发日期。 (1/28...1/31 到 2/28 都相当于 1 个月。)
    3. 第一个经过的月份发生在 1 个整月过去之后。 (当我进行利息计算时,第一个月的利息是在 DateA 上产生的,所以我在这个公式中加了 1。)

    .AddDays(1) 用于说明 DateB 不包含触发日期的月末方案。我想不出一种更优雅、更直接的方法来解决这个问题。

    【讨论】:

      【解决方案4】:

      这方面没有很多明确的答案,因为你总是在假设事情。

      此解决方案计算两个日期之间的月份,假设您要保存月份中的某天以进行比较,(意味着在计算中考虑了月份中的某天)

      例如,如果您的日期是 2012 年 1 月 30 日,则 2012 年 2 月 29 日不是一个月,而是 2013 年 3 月 1 日。

      它已经过非常彻底的测试,可能会在我们使用时清理它,但在这里:

      private static int TotalMonthDifference(DateTime dtThis, DateTime dtOther)
      {
          int intReturn = 0;
          bool sameMonth = false;
      
          if (dtOther.Date < dtThis.Date) //used for an error catch in program, returns -1
              intReturn--;
      
          int dayOfMonth = dtThis.Day; //captures the month of day for when it adds a month and doesn't have that many days
          int daysinMonth = 0; //used to caputre how many days are in the month
      
          while (dtOther.Date > dtThis.Date) //while Other date is still under the other
          {
              dtThis = dtThis.AddMonths(1); //as we loop, we just keep adding a month for testing
              daysinMonth = DateTime.DaysInMonth(dtThis.Year, dtThis.Month); //grabs the days in the current tested month
      
              if (dtThis.Day != dayOfMonth) //Example 30 Jan 2013 will go to 28 Feb when a month is added, so when it goes to march it will be 28th and not 30th
              {
                  if (daysinMonth < dayOfMonth) // uses day in month max if can't set back to day of month
                      dtThis.AddDays(daysinMonth - dtThis.Day);
                  else
                      dtThis.AddDays(dayOfMonth - dtThis.Day);
              }
              if (((dtOther.Year == dtThis.Year) && (dtOther.Month == dtThis.Month))) //If the loop puts it in the same month and year
              {
                  if (dtOther.Day >= dayOfMonth) //check to see if it is the same day or later to add one to month
                      intReturn++;
                  sameMonth = true; //sets this to cancel out of the normal counting of month
              }
              if ((!sameMonth)&&(dtOther.Date > dtThis.Date))//so as long as it didn't reach the same month (or if i started in the same month, one month ahead, add a month)
                  intReturn++;
          }
          return intReturn; //return month
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-02
        相关资源
        最近更新 更多