kyrathasoft

difference in years

Jul 21st, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.56 KB | None | 0 0
  1. public static int GetDifferenceInYears(DateTime startDate, DateTime endDate)
  2. {
  3.         //Excel documentation says "COMPLETE calendar years in between dates"
  4.         int years = endDate.Year - startDate.Year;
  5.  
  6.         if (startDate.Month == endDate.Month &&// if the start month and the end month are the same
  7.             endDate.Day < startDate.Day// AND the end day is less than the start day
  8.             || endDate.Month < startDate.Month)// OR if the end month is less than the start month
  9.         {
  10.             years--;
  11.         }
  12.  
  13.         return years;
  14. }
Advertisement
Add Comment
Please, Sign In to add comment