Difference in months between two dates

前端 未结 30 1185
天命终不由人
天命终不由人 2020-11-22 11:02

How to calculate the difference in months between two dates in C#?

Is there is equivalent of VB\'s DateDiff() method in C#. I need to find difference in

相关标签:
30条回答
  • 2020-11-22 11:41

    You can have a function something like this.

    For Example, from 2012/12/27 to 2012/12/29 becomes 3 days. Likewise, from 2012/12/15 to 2013/01/15 becomes 2 months, because up to 2013/01/14 it's 1 month. from 15th it's 2nd month started.

    You can remove the "=" in the second if condition, if you do not want to include both days in the calculation. i.e, from 2012/12/15 to 2013/01/15 is 1 month.

    public int GetMonths(DateTime startDate, DateTime endDate)
    {
        if (startDate > endDate)
        {
            throw new Exception("Start Date is greater than the End Date");
        }
    
        int months = ((endDate.Year * 12) + endDate.Month) - ((startDate.Year * 12) + startDate.Month);
    
        if (endDate.Day >= startDate.Day)
        {
            months++;
        }
    
        return months;
    }
    
    0 讨论(0)
  • 2020-11-22 11:42

    I wrote a function to accomplish this, because the others ways weren't working for me.

    public string getEndDate (DateTime startDate,decimal monthCount)
    {
        int y = startDate.Year;
        int m = startDate.Month;
    
        for (decimal  i = monthCount; i > 1; i--)
        {
            m++;
            if (m == 12)
            { y++;
                m = 1;
            }
        }
        return string.Format("{0}-{1}-{2}", y.ToString(), m.ToString(), startDate.Day.ToString());
    }
    
    0 讨论(0)
  • 2020-11-22 11:44

    Use Noda Time:

    LocalDate start = new LocalDate(2013, 1, 5);
    LocalDate end = new LocalDate(2014, 6, 1);
    Period period = Period.Between(start, end, PeriodUnits.Months);
    Console.WriteLine(period.Months); // 16
    

    (example source)

    0 讨论(0)
  • 2020-11-22 11:44

    You can use the DateDiff class of the Time Period Library for .NET:

    // ----------------------------------------------------------------------
    public void DateDiffSample()
    {
      DateTime date1 = new DateTime( 2009, 11, 8, 7, 13, 59 );
      DateTime date2 = new DateTime( 2011, 3, 20, 19, 55, 28 );
      DateDiff dateDiff = new DateDiff( date1, date2 );
    
      // differences
      Console.WriteLine( "DateDiff.Months: {0}", dateDiff.Months );
      // > DateDiff.Months: 16
    
      // elapsed
      Console.WriteLine( "DateDiff.ElapsedMonths: {0}", dateDiff.ElapsedMonths );
      // > DateDiff.ElapsedMonths: 4
    
      // description
      Console.WriteLine( "DateDiff.GetDescription(6): {0}", dateDiff.GetDescription( 6 ) );
      // > DateDiff.GetDescription(6): 1 Year 4 Months 12 Days 12 Hours 41 Mins 29 Secs
    } // DateDiffSample
    
    0 讨论(0)
  • 2020-11-22 11:44
    public static int PayableMonthsInDuration(DateTime StartDate, DateTime EndDate)
    {
        int sy = StartDate.Year; int sm = StartDate.Month; int count = 0;
        do
        {
            count++;if ((sy == EndDate.Year) && (sm >= EndDate.Month)) { break; }
            sm++;if (sm == 13) { sm = 1; sy++; }
        } while ((EndDate.Year >= sy) || (EndDate.Month >= sm));
        return (count);
    }
    

    This solution is for Rental/subscription calculation, where difference doesn't means to be subtraction, it's meant to be the span in within those two dates.

    0 讨论(0)
  • 2020-11-22 11:45

    The most precise way is this that return difference in months by fraction :

    private double ReturnDiffereceBetweenTwoDatesInMonths(DateTime startDateTime, DateTime endDateTime)
    {
        double result = 0;
        double days = 0;
        DateTime currentDateTime = startDateTime;
        while (endDateTime > currentDateTime.AddMonths(1))
        {
            result ++;
    
            currentDateTime = currentDateTime.AddMonths(1);
        }
    
        if (endDateTime > currentDateTime)
        {
            days = endDateTime.Subtract(currentDateTime).TotalDays;
    
        }
        return result + days/endDateTime.GetMonthDays;
    }
    
    0 讨论(0)
提交回复
热议问题