Stop Reinventing the Wheel: A QA Pattern for Date Math in User-Facing Code
Every backend that touches a date of birth ends up reinventing the same arithmetic. I've watched it happen in three codebases this year alone: a fintech KYC service, a healthcare intake portal, and a small HR tool for a startup. The bug shape is almost always identical — somebody computes age by subtracting birth year from current year, the QA suite passes because it only tests inputs born in…
Backend systems often repeat the same date arithmetic code, leading to bugs. A quick example is calculating age by subtracting birth year from current year. This fails for people born on February 29 during leap years. The article provides a checklist for engineers to harden date math in user-facing code. The first step is to answer the business two questions: what full years have passed and what duration thresholds have been crossed?
The second step is to commit a matrix of test cases to the code repository that covers various edge cases. The third step is to refactor the age computation so the reference date is a parameter, defaulting to now() only at the public interface. This eliminates the now hidden inside the function being tested. The article also discusses how time-of-day components can affect eligibility rules, recommending to strip the time component at the boundary for comparison.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — it may contain errors, so check the original before relying on it.