Rounding time to nearest quarter hour Rounding time to nearest quarter hour r r

Rounding time to nearest quarter hour


Indeed, an old question with some helpful answers so far. The last one by giraffhere seems to be the most elegant. however, ist not floor_date but round_date which will do the trick:

lubridate::round_date(x, "15 minutes") 


You can use round. The trick is to divide by 900 seconds (15 minutes * 60 seconds) before rounding and multiply by 900 afterwards:

a <-as.POSIXlt("2012-05-30 20:41:21 UTC")b <-as.POSIXlt(round(as.double(a)/(15*60))*(15*60),origin=(as.POSIXlt('1970-01-01')))b[1] "2012-05-30 20:45:00 EDT"

To get only hour and minute, just use format

format(b,"%H:%M")[1] "20:45"as.character(format(b,"%H:%M"))[1] "20:45"


something like

format(strptime("1970-01-01", "%Y-%m-%d", tz="UTC") + round(as.numeric(your.time)/900)*900,"%H:%M")

would work