How to create a date using day, month and year in R?

Viewed 37

Starting from 'data_riferimento' we would to stop the while cycle when data_riferimento is equal to 'data_prova_v'. How to update 'data_riferimento' = 'data_riferimento' + 1?

data_riferimento='08/01/2022'
data_prova_v='11/01/2022'


while (match(data_riferimento,data_prova_v)==NA) {
  giorno_data_riferimento=as.numeric(substring(data_riferimento,1,2))+1
  mese_data_riferimento=as.numeric(substring(data_riferimento,4,5))
  anno_data_riferimento=as.numeric(substring(data_riferimento,7))
  
data_riferimento=as.Date(as.POSIXct(as.numeric(giorno_data_riferimento,mese_data_riferimento,anno_data_riferimento),origin="1970/01/01") --> # it does not work

}

1 Answers

Using lubridate

library(lubridate)

data_riferimento=as_datetime('08/01/2022',tz="UTC",format="%d/%m/%Y")
data_prova_v=as_datetime('11/01/2022',tz="UTC",format="%d/%m/%Y")

while (data_riferimento<=data_prova_v) {
  data_riferimento=data_riferimento+days(1)
}
Related