2019-06-04

How to calculate elapsed and remaining days between dates in R

Problem

We would like to calculate the number of elapsed and remaining days in a year.

Solution

  • From a starting date in the year
  • date<- as.Date("2019-03-29")
    # Elapsed days
    date - as.Date(ISOdate(format(Sys.Date(), "%Y"), 1, 1)) + 1
    
    Time difference of 88 days
    
    # Remaining days
    as.Date(ISOdate(format(Sys.Date(), "%Y"), 12, 31)) - date
    
    Time difference of 277 days
    
  • From today
  • # Elapsed days
    Sys.Date() - as.Date(ISOdate(format(Sys.Date(), "%Y"), 1, 1)) + 1 
    
    Time difference of 153 days
    
    # Remaining days
    as.Date(ISOdate(format(Sys.Date(), "%Y"), 12, 31)) - Sys.Date() 
    
    Time difference of 212 days
    

    Related entries

    No hay comentarios:

    Publicar un comentario

    Nube de datos