Difference in months between two dates

前端 未结 30 1184
天命终不由人
天命终不由人 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:54

    Here is a comprehensive solution to return a DateTimeSpan, similar to a TimeSpan, except that it includes all the date components in addition to the time components.

    Usage:

    void Main()
    {
        DateTime compareTo = DateTime.Parse("8/13/2010 8:33:21 AM");
        DateTime now = DateTime.Parse("2/9/2012 10:10:11 AM");
        var dateSpan = DateTimeSpan.CompareDates(compareTo, now);
        Console.WriteLine("Years: " + dateSpan.Years);
        Console.WriteLine("Months: " + dateSpan.Months);
        Console.WriteLine("Days: " + dateSpan.Days);
        Console.WriteLine("Hours: " + dateSpan.Hours);
        Console.WriteLine("Minutes: " + dateSpan.Minutes);
        Console.WriteLine("Seconds: " + dateSpan.Seconds);
        Console.WriteLine("Milliseconds: " + dateSpan.Milliseconds);
    }
    

    Outputs:

    Years: 1
    Months: 5
    Days: 27
    Hours: 1
    Minutes: 36
    Seconds: 50
    Milliseconds: 0

    For convenience, I've lumped the logic into the DateTimeSpan struct, but you may move the method CompareDates wherever you see fit. Also note, it doesn't matter which date comes before the other.

    public struct DateTimeSpan
    {
        public int Years { get; }
        public int Months { get; }
        public int Days { get; }
        public int Hours { get; }
        public int Minutes { get; }
        public int Seconds { get; }
        public int Milliseconds { get; }
    
        public DateTimeSpan(int years, int months, int days, int hours, int minutes, int seconds, int milliseconds)
        {
            Years = years;
            Months = months;
            Days = days;
            Hours = hours;
            Minutes = minutes;
            Seconds = seconds;
            Milliseconds = milliseconds;
        }
    
        enum Phase { Years, Months, Days, Done }
    
        public static DateTimeSpan CompareDates(DateTime date1, DateTime date2)
        {
            if (date2 < date1)
            {
                var sub = date1;
                date1 = date2;
                date2 = sub;
            }
    
            DateTime current = date1;
            int years = 0;
            int months = 0;
            int days = 0;
    
            Phase phase = Phase.Years;
            DateTimeSpan span = new DateTimeSpan();
            int officialDay = current.Day;
    
            while (phase != Phase.Done)
            {
                switch (phase)
                {
                    case Phase.Years:
                        if (current.AddYears(years + 1) > date2)
                        {
                            phase = Phase.Months;
                            current = current.AddYears(years);
                        }
                        else
                        {
                            years++;
                        }
                        break;
                    case Phase.Months:
                        if (current.AddMonths(months + 1) > date2)
                        {
                            phase = Phase.Days;
                            current = current.AddMonths(months);
                            if (current.Day < officialDay && officialDay <= DateTime.DaysInMonth(current.Year, current.Month))
                                current = current.AddDays(officialDay - current.Day);
                        }
                        else
                        {
                            months++;
                        }
                        break;
                    case Phase.Days:
                        if (current.AddDays(days + 1) > date2)
                        {
                            current = current.AddDays(days);
                            var timespan = date2 - current;
                            span = new DateTimeSpan(years, months, days, timespan.Hours, timespan.Minutes, timespan.Seconds, timespan.Milliseconds);
                            phase = Phase.Done;
                        }
                        else
                        {
                            days++;
                        }
                        break;
                }
            }
    
            return span;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 11:54

    This worked for what I needed it for. The day of month didn't matter in my case because it always happens to be the last day of the month.

    public static int MonthDiff(DateTime d1, DateTime d2){
        int retVal = 0;
    
        if (d1.Month<d2.Month)
        {
            retVal = (d1.Month + 12) - d2.Month;
            retVal += ((d1.Year - 1) - d2.Year)*12;
        }
        else
        {
            retVal = d1.Month - d2.Month;
            retVal += (d1.Year - d2.Year)*12;
        }
        //// Calculate the number of years represented and multiply by 12
        //// Substract the month number from the total
        //// Substract the difference of the second month and 12 from the total
        //retVal = (d1.Year - d2.Year) * 12;
        //retVal = retVal - d1.Month;
        //retVal = retVal - (12 - d2.Month);
    
        return retVal;
    }
    
    0 讨论(0)
  • 2020-11-22 11:54

    In my case it is required to calculate the complete month from the start date to the day prior to this day in the next month or from start to end of month.


    Ex: from 1/1/2018 to 31/1/2018 is a complete month
    Ex2: from 5/1/2018 to 4/2/2018 is a complete month

    so based on this here is my solution:

    public static DateTime GetMonthEnd(DateTime StartDate, int MonthsCount = 1)
    {
        return StartDate.AddMonths(MonthsCount).AddDays(-1);
    }
    public static Tuple<int, int> CalcPeriod(DateTime StartDate, DateTime EndDate)
    {
        int MonthsCount = 0;
        Tuple<int, int> Period;
        while (true)
        {
            if (GetMonthEnd(StartDate) > EndDate)
                break;
            else
            {
                MonthsCount += 1;
                StartDate = StartDate.AddMonths(1);
            }
        }
        int RemainingDays = (EndDate - StartDate).Days + 1;
        Period = new Tuple<int, int>(MonthsCount, RemainingDays);
        return Period;
    }
    

    Usage:

    Tuple<int, int> Period = CalcPeriod(FromDate, ToDate);
    

    Note: in my case it was required to calculate the remaining days after the complete months so if it's not your case you could ignore the days result or even you could change the method return from tuple to integer.

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

    I checked the usage of this method in VB.NET via MSDN and it seems that it has a lot of usages. There is no such a built-in method in C#. (Even it's not a good idea) you can call VB's in C#.

    1. Add Microsoft.VisualBasic.dll to your project as a reference
    2. use Microsoft.VisualBasic.DateAndTime.DateDiff in your code
    0 讨论(0)
  • 2020-11-22 11:58

    This simple static function calculates the fraction of months between two Datetimes, e.g.

    • 1.1. to 31.1. = 1.0
    • 1.4. to 15.4. = 0.5
    • 16.4. to 30.4. = 0.5
    • 1.3. to 1.4. = 1 + 1/30

    The function assumes that the first date is smaller than the second date. To deal with negative time intervals one can modify the function easily by introducing a sign and a variable swap at the beginning.

    public static double GetDeltaMonths(DateTime t0, DateTime t1)
    {
         DateTime t = t0;
         double months = 0;
         while(t<=t1)
         {
             int daysInMonth = DateTime.DaysInMonth(t.Year, t.Month);
             DateTime endOfMonth = new DateTime(t.Year, t.Month, daysInMonth);
             int cutDay = endOfMonth <= t1 ? daysInMonth : t1.Day;
             months += (cutDay - t.Day + 1) / (double) daysInMonth;
             t = new DateTime(t.Year, t.Month, 1).AddMonths(1);
         }
         return Math.Round(months,2);
     }
    
    0 讨论(0)
  • 2020-11-22 11:59

    To be able to calculate the difference between 2 dates in months is a perfectly logical thing to do, and is needed in many business applications. The several coders here who have provided comments such as - what's the difference in months between "May 1,2010" and "June 16,2010, what's the difference in months between 31 December 2010 and 1 Jan 2011? -- have failed to understand the very basics of business applications.

    Here is the answer to the above 2 comments - The number of months between 1-may-2010 and 16-jun-2010 is 1 month, the number of months between 31-dec-2010 and 1-jan-2011 is 0. It would be very foolish to calculate them as 1.5 months and 1 second, as the coders above have suggested.

    People who have worked on credit card, mortgage processing, tax processing, rent processing, monthly interest calculations and a vast variety of other business solutions would agree.

    Problem is that such a function is not included in C# or VB.NET for that matter. Datediff only takes into account years or the month component, so is actually useless.

    Here are some real-life examples of where you need to and correctly can calculate months:

    You lived in a short-term rental from 18-feb to 23-aug. How many months did you stay there? The answer is a simple - 6 months

    You have a bank acount where interest is calculated and paid at the end of every month. You deposit money on 10-jun and take it out 29-oct (same year). How many months do you get interest for? Very simple answer- 4 months (again the extra days do not matter)

    In business applications, most of the time, when you need to calculate months, it is because you need to know 'full' months based on how humans calculate time; not based on some abstract/irrelevant thoughts.

    0 讨论(0)
提交回复
热议问题