Signum.Utilities.DateSpan.FromToDates C# (CSharp) Method

FromToDates() public static method

public static FromToDates ( System.DateTime min, System.DateTime max ) : DateSpan
min System.DateTime
max System.DateTime
return DateSpan
        public static DateSpan FromToDates(DateTime min, DateTime max)
        {
            if (min > max) return FromToDates(max, min).Invert();

            int years = max.Year - min.Year;
            int months = max.Month - min.Month;


            if (max.Day < min.Day)
                months -= 1;

            if (months < 0)
            {
                years -= 1;
                months += 12;
            }

            int days = max.Subtract(min.AddYears(years).AddMonths(months)).Days;

            return new DateSpan(years, months, days);
        }

Usage Example

Ejemplo n.º 1
0
 public static DateSpan DateSpanTo(this DateTime min, DateTime max)
 {
     return(DateSpan.FromToDates(min, max));
 }