Getting previous month start date and end date from current date in R

后端 未结 6 1579
心在旅途
心在旅途 2020-12-29 03:55

Is there any easy way for getting start date and end date of previous month from the current date in R?

I have only the current date. From it, i want to get the prev

6条回答
  •  醉酒成梦
    2020-12-29 04:48

    You can use the library lubridate, which is very good at doing date arithmetic.

    library(lubridate)
    
    currentDate <-Sys.Date()
    # end of previous month:
    eopm <- currentDate - days(day(currentDate))
    # [1] "2012-10-31"
    
    # start of previous month:
    sopm <- currentDate - days(day(currentDate))
    sopm <- sopm - days(day(sopm) - 1)
    # [1] "2012-10-01"
    

提交回复
热议问题