The time delta (in years) between the given start and end dates - CSharp System

CSharp examples for System:DateTime Year

Description

The time delta (in years) between the given start and end dates

Demo Code


using System.Collections;
using System;//from   ww w  .  ja  v  a 2  s  .  c o  m

public class Main{
        /// <summary>
        /// The time delta (in years) between the given start and end dates
        /// </summary>
        /// <param name="begin">The DateTime object representing the start</param>
        /// <param name="end">The DateTime object represending the end</param>
        /// <returns>A double representing the total number of years between the two dates</returns>
        public static double DeltaYears(DateTime begin, DateTime end) {
            TimeSpan start = new TimeSpan(begin.Ticks);
            TimeSpan stop = new TimeSpan(end.Ticks);
            TimeSpan diff = stop.Subtract(start);
            return diff.TotalDays / 365;
        }
}

Related Tutorials