Rounding of dates
There might be situations where we have a date/time and a need to compute the previous, or next complete time interval, for example, the next hour, or the previous day. The Dates
API exposes a few methods for rounding Date
and DateTime
objects—floor
, ceil
, and time
. They are quite intuitive and very powerful:
julia> now() 2018-11-08T21:13:20.605 # round down to the nearest hour julia> floor(now(), Hour) 2018-11-08T21:00:00 # or to the nearest 30 minutes increment julia> floor(now(), Minute(30)) 2018-11-08T21:00:00 # it also works with dates julia> floor(today(), Month) # today() is the 8th of Nov 2018 2018-11-01
The ceil
function works similarly, but instead of rounding down, it rounds up. As for the round
function, it will round up or down, depending on whichever is the closest value:
julia> round(today(), Month)
2018-11-01 # today is the 11th so beginning of month is closer
julia> round(today() + Day(10), Month)
2018-12-01 # end of month...