Skip to content
Prev 199555 / 398502 Next

Turn dates into age

[1] "2009-11-08"

 > as.Date("05/29/1971", format = "%m/%d/%Y")
[1] "1971-05-29"


 > as.numeric((Sys.Date() - as.Date("05/29/1971", format = "%m/%d/ 
%Y")) / 365.25)
[1] 38.44764

or perhaps more clearly:

EndDate <- Sys.Date()
StartDate <- as.Date("05/29/1971", format = "%m/%d/%Y")

 > as.numeric((EndDate - StartDate) / 365.25)
[1] 38.44764



We coerce to numeric here, to return a standard numeric value, rather  
than the result being of class difftime with an attribute of 'days'  
for units:

 > str((Sys.Date() - as.Date("05/29/1971", format = "%m/%d/%Y")) /  
365.25)
Class 'difftime'  atomic [1:1] 38.4
   ..- attr(*, "units")= chr "days"



HTH,

Marc Schwartz
On Nov 8, 2009, at 5:22 PM, Jim Burke wrote: