How can I round a date to the quarter start/end?

后端 未结 3 1965
夕颜
夕颜 2021-01-04 22:45

I need to take a vector of dates and for each date get the first day of the next quarter.

(Or rather, to round to the last day of the current quarter, find the first

3条回答
  •  再見小時候
    2021-01-04 23:27

    The zoo package can help for many things date-related including this:

    library(zoo)
    
    as.yearqtr("2014-08-15", format="%Y-%m-%d")
    ## [1] "2014 Q3"
    
    as.Date(as.yearqtr("2014-08-15", format="%Y-%m-%d"))
    ## [1] "2014-07-01"
    

    But, that might not get you what you need (there are ways to extrapolate from those values).

    The timeDate package has:

    timeFirstDayInQuarter(charvec, format = "%Y-%m-%d", zone = "", FinCenter = "")
    timeLastDayInQuarter(charvec, format = "%Y-%m-%d", zone = "", FinCenter = "")
    

    which might make it easer to use and tweak to adjust for different Q1 start origins.

提交回复
热议问题