Coder Perfect

Between two dates, there is a month difference.

Problem

In C#, how do you determine the month difference between two dates?

Is there a C# counterpart of the VB DateDiff() method? I’m looking for a month gap between two dates that are years apart. According to the documentation, I can use TimeSpan in the following ways:

TimeSpan ts = date1 - date2;

However, this provides me with data in days. I don’t want to divide this number by 30 because not every month has 30 days, and because the two operand values are so dissimilar, I’m worried dividing by 30 will result in an incorrect result.

Any suggestions?

Asked by Rauf

Solution #1

Assuming that the day of the month is immaterial (i.e. the difference between 2011.1.1 and 2010.12.31 is 1), date1 > date2 gives a positive value and date2 > date1 gives a negative value, with date1 > date2 giving a positive value and date2 > date1 offering a negative value.

((date1.Year - date2.Year) * 12) + date1.Month - date2.Month

Alternatively, if you want an estimate of the number of ‘typical months’ between two dates, the following formula should work for all but the most extreme date disparities.

date1.Subtract(date2).Days / (365.25 / 12)

If you choose the latter option, your unit tests should specify the broadest date range that your application is meant to support and validate the calculation results accordingly.

new information (with thanks to Gary)

365.2425 is a somewhat more accurate value to use for the ‘average number of days per year’ when utilizing the ‘average months’ method.

Answered by Adam Ralph

Solution #2

Here’s a complete method for returning a DateTimeSpan, which is similar to a TimeSpan but includes all of the date components as well as 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:

I’ve grouped the logic under the DateTimeSpan struct for ease of use, but you may place the method CompareDates wherever you want. It’s also worth noting that it doesn’t matter which date comes first.

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;
    }
}

Answered by Kirk Woll

Solution #3

You could do

if ( date1.AddMonths(x) > date2 )

Answered by Mongus Pong

Solution #4

If you want the exact number of full months, always positive (2000-01-15, 2000-02-14 returns 0), considering a full month is when you reach the same day the next month (something like the age calculation)

public static int GetMonthsBetween(DateTime from, DateTime to)
{
    if (from > to) return GetMonthsBetween(to, from);

    var monthDiff = Math.Abs((to.Year * 12 + (to.Month - 1)) - (from.Year * 12 + (from.Month - 1)));

    if (from.AddMonths(monthDiff) > to || to.Day < from.Day)
    {
        return monthDiff - 1;
    }
    else
    {
        return monthDiff;
    }
}

Edit reason: In some circumstances, the old code was incorrect, such as:

new { From = new DateTime(1900, 8, 31), To = new DateTime(1901, 8, 30), Result = 11 },

Test cases I used to test the function:

var tests = new[]
{
    new { From = new DateTime(1900, 1, 1), To = new DateTime(1900, 1, 1), Result = 0 },
    new { From = new DateTime(1900, 1, 1), To = new DateTime(1900, 1, 2), Result = 0 },
    new { From = new DateTime(1900, 1, 2), To = new DateTime(1900, 1, 1), Result = 0 },
    new { From = new DateTime(1900, 1, 1), To = new DateTime(1900, 2, 1), Result = 1 },
    new { From = new DateTime(1900, 2, 1), To = new DateTime(1900, 1, 1), Result = 1 },
    new { From = new DateTime(1900, 1, 31), To = new DateTime(1900, 2, 1), Result = 0 },
    new { From = new DateTime(1900, 8, 31), To = new DateTime(1900, 9, 30), Result = 0 },
    new { From = new DateTime(1900, 8, 31), To = new DateTime(1900, 10, 1), Result = 1 },
    new { From = new DateTime(1900, 1, 1), To = new DateTime(1901, 1, 1), Result = 12 },
    new { From = new DateTime(1900, 1, 1), To = new DateTime(1911, 1, 1), Result = 132 },
    new { From = new DateTime(1900, 8, 31), To = new DateTime(1901, 8, 30), Result = 11 },
};

Answered by Guillaume86

Solution #5

I looked up the usage of this function in VB.NET on MSDN, and it appears to have a lot of them. In C#, there isn’t such a thing as a built-in method. You can call VB’s in C# (even if it’s not a smart idea).

Answered by Cheng Chen

Post is based on https://stackoverflow.com/questions/4638993/difference-in-months-between-two-dates