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

I know it’s an old thread but I found my way here from a recent duplicate post.

lubridate::ceiling_date() now accepts quarters.


For any time interval (Annual, Quarterly, Monthly, Weekly), the following function gives the end date, using R base functions (as.POSIXlt and as.Date):

endDate <- function(date, interval) {
    date.lt <- as.POSIXlt(date) 
    switch(interval, 
           A = {
                date.lt$mon = 11
                date.lt$mday=31
                date=as.Date(date.lt)
            },
           Q = {
                date.lt$mon = (date.lt$mon %/% 3 +1)*3 %% 12 
                date.lt$mday = 1
                date.lt$year = date.lt$year + as.integer(date.lt$mon==0)
                date=as.Date(date.lt)-1
            },
           M = {
                date.lt$mon = (date.lt$mon+1) %% 12
                date.lt$mday = 1
                date.lt$year = date.lt$year + as.integer(date.lt$mon==0)
                date=as.Date(date.lt)-1
            },
           D = {
               date = as.Date(date)
            },
           W = {
               date = as.Date(date)+6-date.lt$wday
           },
           date = as.Date(date$lt)
    )
    date
}
###
endDate (c("2003-05-21","1945-03-29), "M")
# "2003-05-31" "1945-03-31"
endDate (c("2003-05-21","1945-03-29), "M")
#"2003-06-30" "1945-03-31"

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.

Tags:

Date

Rounding

R