It would be better to convert to Date
class as the 2-digit year can be an issue when we extend the years.
library(chron)
date1 <- as.Date("01/31/80", format = "%m/%d/%y")
date2 <- as.Date("01/03/30", format = "%m/%d/%y")
here, the conversion is correct
date1
#[1] "1980-01-31"
date2
#[1] "2030-01-03"
Based on the ?seq.dates
, either we can pass a character
string or numeric
value (convert the 'Date' class to 'numeric'
length(seq.dates(as.numeric(date1), as.numeric(date2), by = "months"))
#[1] 600
Or a julian
date
j1 <- julian(date1, origin = as.Date('1970-01-01'))
j2 <- julian(date2, origin = as.Date('1970-01-01'))
length(seq.dates(j1, j2, by = 'months'))
#[1] 600
Or use a 4-digit year in character
format
length(chron::seq.dates("01/31/1980", "01/03/2030", by="months"))
#[1] 600
If the dates are already available in 2 digits, can insert the specific digits with sub
sub("(\\d+)$", "20\\1", "01/03/30")
#[1] "01/03/2030"
and pass that value in the seq.dates
length(seq.dates("01/31/80", sub("(\\d+)$", "20\\1", "01/03/30"), by = "months"))
#[1] 600