Time Calculator: Quick Framework
1. Core Ops
- Add/Subtract durations (D,H,M,S → total seconds → normalize).
- Date shift = base UTC ms ± delta (avoid manual month arithmetic except via Date API).
- Expression parser: scan tokens (+|-) then unit groups (d/h/m/s).
2. Flow
- Collect inputs → sanitize (blank=0).
- Convert everything to seconds.
- Apply sign / combine.
- Format back: days, hours, minutes, seconds (largest units first).
3. Sanity Checks
- Negative final? Flag but still show magnitude.
- Hours 0–23, minutes/seconds 0–59 (after normalization).
- Date diff: end >= start else error message.
4. Shortcuts
- Compare durations using raw seconds (skip formatting).
- Need only ordering? Use seconds; skip decomposition.
- Batch adds: pre-sum seconds arrays then format once.
5. Pitfalls
- Mixing 24h vs 12h without AM/PM transform.
- Dropping sign on subtraction result.
- Manual month length assumptions (use Date object).
6. Micro Examples
(1d 2h 30m) + (4h 5m) = 1d 6h 35m
2025‑01‑01 12:00 + 90m → 2025‑01‑01 13:30
(3h − 5h) → Negative: 2h
7. Mini FAQ
- Why decimal hours? Payroll & rate multipliers.
- Keep leading zeros? Only for fixed-format output.
- Large spans? Use BigInt ms if beyond JS Date limit.
8. Action Tip
Store canonical value once (seconds) alongside formatted view to avoid repeated reconversion on UI tweaks.