0

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?

  • What is `formatter.dateFormat`? – ielyamani Sep 06 '18 at 14:26
  • @Carpsen90 Added the formatter code – Naveen George Thoppan Sep 06 '18 at 14:28
  • I am getting this `15.1068` in `years1` – ielyamani Sep 06 '18 at 14:30
  • 1
    When reviewing the duplicate, be sure to read the comments below the question. This question is actually a lot more complex and ambiguous than it first seems. – rmaddy Sep 06 '18 at 14:38
  • There are many conventions to define the difference between two dates as a fractional number of years, see for example https://en.wikipedia.org/wiki/Day_count_convention. Can you clarify which convention you need? – Martin R Sep 06 '18 at 14:40
  • BTW - How was the required result of 15.421629021218 calculated? – rmaddy Sep 06 '18 at 14:45
  • 1
    Also, the text of your question appears to be based on June 2, 2016 (that would give an answer just under 15 1/2) but the code in your question is based on January 2, 2016. That makes for a large, confusing difference. – rmaddy Sep 06 '18 at 14:55
  • Even from Jan 1, 2001 to June 2, 2016 (which is 15 years, 5 month, 1 day, or 5631 days) I fail to get the required 15.421629021218 with any factor of (360, 365, 366, 365.2422) – Martin R Sep 06 '18 at 15:36
  • It's just impossible because "year" is not a constant. The standard unit of measurement for time is "second", which is a constant. But a year can have a leap second, a different number of days and not all days have 24 hours (see Daylight Savings Time). – Code Different Sep 06 '18 at 18:24
  • @rmaddy I follow an excel sheet where they calculated the time difference in years as 15.421629021218. – Naveen George Thoppan Sep 07 '18 at 09:30
  • @NaveenGeorgeThoppan: Is your first date 01/01/2001 UTC or 01/01/2001 in your time zone? Is the second date January 2, 2016 or June 2, 2016? What is the exact Excel formula that you are using? – Martin R Sep 07 '18 at 11:17
  • See https://stackoverflow.com/questions/8565255/emulating-excels-yearfrac-in-c-sharp and https://lists.oasis-open.org/archives/office-formula/200806/msg00039.html – rmaddy Sep 07 '18 at 15:08
  • I got a very close answer to that of YEARFRAC by calculating timeIntervalSinceReferenceDate and dividing the time in seconds by 520729200 which is equal to 365.25 days. This was more accurate than using 365.24. – Naveen George Thoppan Sep 07 '18 at 16:28

0 Answers0