View source: R/age_calculate.R
age_calculate  R Documentation 
This function calculates the age between two dates using
functions in lubridate
. It calculates age in either years or months.
age_calculate(
start,
end = if (lubridate::is.Date(start)) Sys.Date() else Sys.time(),
units = c("years", "months"),
round_down = TRUE
)
start 
A start date (e.g. date of birth) which must be supplied with

end 
An end date which must be supplied with 
units 
Type of units to be used. years and months are accepted.
Default is 
round_down 
Should returned ages be rounded down to the nearest whole
number. Default is 
A numeric vector representing the ages in the given units.
library(lubridate)
birth_date < lubridate::ymd("20200229")
end_date < lubridate::ymd("20220221")
age_calculate(birth_date, end_date)
age_calculate(birth_date, end_date, units = "months")
# If the start day is leap day (February 29th), age increases on 1st March
# every year.
leap1 < lubridate::ymd("20200229")
leap2 < lubridate::ymd("20220228")
leap3 < lubridate::ymd("20220301")
age_calculate(leap1, leap2)
age_calculate(leap1, leap3)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.