Skip to Content

convert difftime time to years, months and days

How can I accurately convert the products (units is in days) of the difftime below to years, months and days?

difftime(Sys.time(),"1931-04-10")
difftime(Sys.time(),"2012-04-10")

This does years and days but how could I include months?

yd.conv<-function(days, print=TRUE){
    x<-days*0.00273790700698851
    x2<-floor(x)
    x3<-x-x2
    x4<-floor(x3*365.25)
    if (print) cat(x2,"years &",x4,"days\n")
    invisible(c(x2, x4))
}
 
yd.conv(difftime(Sys.time(),"1931-04-10"))
yd.conv(difftime(Sys.time(),"2012-04-10"))

I'm not sure how to even define months either. Would 4 weeks be considered a month or the passing of the same month day. So for the later definition of a month if the initial date was 2012-01-10 and the current 2012-05-31 then we'd have 0 years, 5 months and 21 days. This works well but what if the original date was on the 31st of the month and the end date was on feb 28 would this be considered a month?

As I wrote this question the question itself evolved so I'd better clarify:

What would be the best (most logical approach) to defining months and then how to find diff time in years, months and days?