# Rounding datetime objects to a given granularity

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.

Assuming DatePlus handles adding fractional units properly (which I'm pretty sure it does), I would try adding half the unit you want and then doing the floor operation that CurrentDate does. It works for your examples.
dateRound[d_, s_] := CurrentDate[DatePlus[d, {.5, s}], s]