Calendar Date Rounder
Round any date and time to the nearest minute, hour, day, week, month, quarter, or year with configurable rounding direction.
About
Rounding a date to a coarser calendar unit is a deceptively complex operation. Minutes and hours follow uniform 60-second and 3600-second cycles, so modular arithmetic on a Unix timestamp suffices. Days, however, are sensitive to timezone offset. Weeks depend on your epoch weekday definition (ISO 8601 designates Monday as day 1). Months have variable lengths (28 - 31 days), making the midpoint ambiguous. Quarters and years compound that irregularity. Getting this wrong in financial reporting, log aggregation, or scheduling systems causes silent data misalignment - transactions attributed to the wrong period, dashboards showing shifted trends, cron jobs firing a day late. This tool implements calendar-aware rounding with three direction modes (nearest, ceiling, floor) across seven granularity levels from min to yr.
Limitation: all calculations assume the Gregorian calendar and do not account for leap seconds. Week rounding follows ISO 8601 (Monday start). The midpoint rule for "nearest" mode uses round-half-up convention. Pro tip: when rounding timestamps for database GROUP BY queries, always round in the same timezone your data was recorded in to avoid off-by-one period errors near DST transitions.
Formulas
For uniform sub-day units (minute, hour, and their multiples), rounding uses modular arithmetic on the Unix timestamp:
where t is the Unix timestamp in milliseconds and U is the unit size in milliseconds (e.g., 3600000 for one hour). The round function is replaced by floor for rounding down or ceil for rounding up.
For calendar-variable units (month, quarter, year), the algorithm computes two candidate boundaries - the floor boundary and the ceiling boundary - then selects based on direction mode:
The closer function compares |t − tfloor| against |tceil − t|. On exact midpoint, the ceiling (round-half-up) is chosen.
Where t = input timestamp (ms), U = unit duration (ms), tfloor = start of current period, tceil = start of next period.
Reference Data
| Unit | Cycle Length | Midpoint (Nearest) | Floor Snaps To | Ceil Snaps To | Notes |
|---|---|---|---|---|---|
| Minute | 60 s | 30 s | :00 of current min | :00 of next min | Uniform cycle |
| Hour | 3600 s | 30 min | :00:00 of current hr | :00:00 of next hr | Uniform cycle |
| Day | 86400 s | 12:00 noon | 00:00:00 of current day | 00:00:00 of next day | DST days may be 23 or 25 h |
| Week (ISO) | 7 d | Thursday 00:00 | Monday 00:00 of current wk | Monday 00:00 of next wk | ISO 8601: Mon = day 1 |
| Month | 28 - 31 d | ~15th - 16th | 1st of current month | 1st of next month | Variable length |
| Quarter | 3 months | ~mid-month 2 | 1st of Q start (Jan/Apr/Jul/Oct) | 1st of next Q | Q1=Jan, Q2=Apr, Q3=Jul, Q4=Oct |
| Year | 365 - 366 d | ~Jul 2 | Jan 1 of current year | Jan 1 of next year | Leap year aware |
| 5 Minutes | 300 s | 150 s | :X0/:X5 of current | :X0/:X5 of next | Common in billing |
| 15 Minutes | 900 s | 7.5 min | :00/:15/:30/:45 | Next quarter-hour | Standard time-tracking interval |
| 30 Minutes | 1800 s | 15 min | :00/:30 | Next half-hour | Meeting scheduling default |
| 6 Hours | 21600 s | 3 h | 00:00/06:00/12:00/18:00 | Next 6-hour block | Synoptic meteorology intervals |
| 12 Hours | 43200 s | 6 h | 00:00/12:00 | Next half-day | AM/PM boundary |