Advertisement
kyrathasoft

whole years between dates

May 21st, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.49 KB | None | 0 0
  1. public static int GetDiffInWholeYears(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
Advertisement