Difference in months between two dates

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

    Expanded Kirks struct with ToString(format) and Duration(long ms)

     public struct DateTimeSpan
    {
        private readonly int years;
        private readonly int months;
        private readonly int days;
        private readonly int hours;
        private readonly int minutes;
        private readonly int seconds;
        private readonly int milliseconds;
    
        public DateTimeSpan(int years, int months, int days, int hours, int minutes, int seconds, int milliseconds)
        {
            this.years = years;
            this.months = months;
            this.days = days;
            this.hours = hours;
            this.minutes = minutes;
            this.seconds = seconds;
            this.milliseconds = milliseconds;
        }
    
        public int Years { get { return years; } }
        public int Months { get { return months; } }
        public int Days { get { return days; } }
        public int Hours { get { return hours; } }
        public int Minutes { get { return minutes; } }
        public int Seconds { get { return seconds; } }
        public int Milliseconds { get { return milliseconds; } }
    
        enum Phase { Years, Months, Days, Done }
    
    
        public string ToString(string format)
        {
            format = format.Replace("YYYY", Years.ToString());
            format = format.Replace("MM", Months.ToString());
            format = format.Replace("DD", Days.ToString());
            format = format.Replace("hh", Hours.ToString());
            format = format.Replace("mm", Minutes.ToString());
            format = format.Replace("ss", Seconds.ToString());
            format = format.Replace("ms", Milliseconds.ToString());
            return format;
        }
    
    
        public static DateTimeSpan Duration(long ms)
        {
            DateTime dt = new DateTime();
            return CompareDates(dt, dt.AddMilliseconds(ms));
        }
    
    
        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();
    
            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);
                        }
                        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;
        }
    }
    

提交回复
热议问题