I would like to calculate the time difference between two dates in years. I am not looking for using date components as I need the exact year difference between two dates.
For example: Reference Date is 01/01/2001
Date 1 = 06/02/2016
Required Output is 15.421629021218 (As in the Excel file I'm referring)
MS Excel has an YEARFRAC formula to calculate year difference between two dates. I'm looking for something similar to it as I'm trying to create a Swift program to replicate what I have in the excel file.
I use the following code now to calculate the time difference
let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yyyy"
let date1 = formatter.date(from: "02/01/2016")
let difference1 = date1?.timeIntervalSinceReferenceDate
let hours1 = difference1!/3600
let days1 = hours1/24
let years1 = days1/365
Division by 365 for calculating years seems to be an issue as there are leap years in between. Can someone help me to sort this issue?