How does one round datetime objects to any granularity (eg "Hour","Minute" etc)?
What I'm looking for is a function that would do the following:
DateRound[DateObject[{2018, 7, 7, 10, 55, 42.}], "Hour"]
-> DateObject[{2018, 7, 7, 11, 0, 0.}]
DateRound[DateObject[{2018, 7, 7, 10, 55, 42.}], "Minute"]
-> DateObject[{2018, 7, 7, 10, 56, 0.}]
That is, round the given DateObject to whatever granularity is given - the nearest minute, nearest hour, day, month etc.
I can Floor dates using CurrentDate
:
CurrentDate[DateObject[{2018, 7, 7, 10, 55, 42.}], "Hour"]
-> DateObject[{2018, 7, 7, 10}]
or by using DateValue
:
DateValue[DateObject[{2018, 7, 7, 10, 55, 42.}], {"Year", "Month", "Day", "Hour"}]
-> {2018, 7, 7, 10}
but as you can see, this does not give me the rounded granularity (11), but the floored granularity (10).
I know about DayRound
, which is approximately the functionality I'm looking for, but it doesn't support any time operations.