Quantcast
Viewing all articles
Browse latest Browse all 46

Answer by Kirk Woll for Difference in months between two dates

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

Viewing all articles
Browse latest Browse all 46

Trending Articles