-1

I'm trying to calculate the difference-in-years between 2 dates.

time1 <- as.Date(x = "2017-02-14",
                 format = "%Y-%m-%d",
                 origin = "",
                 tz = "")

time2 <- as.Date(x = "1972-02-17",
                 format = "%Y-%m-%d",
                 origin = "",
                 tz = "")

as.integer(x = difftime(time1 = time1,
                        time2 = time2,
                        tz = "",
                        units = "days")) / 365

According to the above code, the difference-in-years is about 45.02466.

A glance at the 2 dates confirms that the difference-in-years is close to, but less than, 45.

The difference-in-years value should begin with 44.

Why is difftime inflating the number of years by 1?

zhaoy
  • 332
  • 1
  • 7
  • 15

2 Answers2

1

Probably this happens since you divide by 365 days, whereas at least 44/4 = 11 years had a duration of 366 days. You could consider to divide by 365.25, which is a more correct average duration of a year.

Also, you might like to look up the lubridate package to see the interval of how many years have passed between two dates

library(lubridate)
> time1 <- as.Date(x = "2017-02-14",
+                  format = "%Y-%m-%d",
+                  origin = "",
+                  tz = "")
> 
> time2 <- as.Date(x = "1972-02-17",
+                  format = "%Y-%m-%d",
+                  origin = "",
+                  tz = "")
> 
> 
> interval(time2, time1) %/% years(1)
[1] 44
Lennyy
  • 5,932
  • 2
  • 10
  • 23
1

There are an average of 365.25 days in a year due to leap years -- could be slightly different if the number of years is not a multiple of 4 but this is a reasonable long term average to use. By using 365 instead this inflated the answer in the question.

time1 <- as.Date("2017-02-14")
time2 <- as.Date("1972-02-17")
as.numeric(time1 - time2) / 365.25
## [1] 44.99384

Standardized years

If you want to be really exact about leap years there were 12 leap days between time1 and time2

tt <- seq(time2, time1, by = "day")
no_leap_days <- sum(format(tt, "%m-%d") == "02-29")
no_leap_days
## [1] 12

so another way to do this is to calculate the number of days between the two dates, subtract the number of leap days effectively standardizing every year to 365 days and then divide by 365:

as.numeric(time1 - time2 - no_leap_days) / 365
## [1] 44.99178

Note

Note that "Date" class does not have a time zone, origin is only used in as.Date if the argument is numeric and the format used in the question is the default format so format is not needed.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341