The internal representation of "Date"
class is the number of days since the Epoch. (If you have a POSIXct variable then convert it to Date class using as.Date first however watch out for time zone conversion problems.)
now <- Sys.Date() # Date
d <- as.numeric(now) # days since Epoch
Now use the facts that there are an average of 365.25 days per year and 365.25 / 12 days per month to get the years and months since the Epoch.
m <- d / (365.25 / 12) # months since Epoch
y <- d / 365.25 # years since Epoch
years/months/days
If we did not want years, months and days separately but rather want the number of whole years and months (0-11) and days (0-30) then use POSIXlt:
lt <- as.POSIXlt(format(Sys.Date()))
with(lt, data.frame(years = year - 70, months = mon - 0, days = mday - 1))
This also works if lt
is a vector, e.g. lt <- c(lt, lt)
.