I found out about it from this old stackoverflow answer by hadley wickham. The real gem is the function floor date from the lubridate package. Floor date dt unit month round date x unit second round to nearest unit.