How to Calculate Age Correctly: The Edge Cases Every Developer Misses
javascript
dev.to
Calculating someone's age sounds like a five-minute task: function getAge(birthday) { const diff = Date.now() - new Date(birthday).getTime(); return Math.floor(diff / (365.25 * 24 * 60 * 60 * 1000)); } Ship it. Except this gives wrong answers on certain dates and can be off by a full year around birthdays. Let's fix it. Why the Division Approach Fails Dividing milliseconds by an "average year" (365.25 days) is imprecise because: Leap years don't distribute uniformly —