How do I calculate someone's age based on a DateTime type birthday?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Age calculation looks simple until calendar edge cases appear. If your input is a DateTime birthday, the safest approach is to compare the current date against the birth month and day rather than dividing elapsed days by a constant.
The Core Rule For Age
Age is not "current year minus birth year" by itself. You subtract one more year if the birthday has not happened yet in the current year.
A correct C# implementation looks like this:
The birthday.AddYears(age) comparison is a clean way to ask whether this year's birthday has already occurred.
Why Day Counts Are A Bad Shortcut
Developers sometimes try this:
That looks convenient, but it is not correct because:
- leap years change the day count
- a birthday may not have happened yet this year
- time-of-day can create off-by-one confusion if you use
DateTime.Now
Age is a calendar concept, not a fixed number of elapsed 24-hour blocks.
Use DateTime.Today If Time Does Not Matter
If you are calculating legal or profile age, the time of day usually does not matter. Prefer DateTime.Today over DateTime.Now:
DateTime.Today strips away the current time component and makes the comparison easier to reason about.
If your birthday value includes a time, that is usually fine as long as you consistently compare against a date-oriented "today" value.
Watch Time Zones And Stored UTC Values
If birthdays are stored as UTC timestamps, convert carefully before applying local calendar rules. A birthday represented as midnight UTC can become the previous local date in another timezone.
For example, if the business rule is "age according to the user's local date," then the date normalization should happen before the age calculation.
That is one reason many systems store birthdays as date-only values instead of full timestamps.
Prefer Date-Only Types When Possible
If you control the model and do not need a time component, a date-only type is better than DateTime. In modern .NET, DateOnly expresses the real intent:
This avoids accidental timezone and time-of-day bugs entirely.
Still, if the existing data type is DateTime, the logic remains the same: compare year difference and adjust if the birthday has not yet occurred.
Test Boundary Cases
Age calculations should always be tested around the birthday boundary:
That is where most buggy implementations fail.
Leap-day birthdays also deserve explicit tests. Different businesses may handle February 29 birthdays differently in non-leap years, so confirm the policy if the answer matters legally or commercially.
Common Pitfalls
One common mistake is dividing elapsed days by 365 and treating that as an age calculation. It is only an approximation.
Another issue is using DateTime.Now when only the calendar date matters, which adds unnecessary time-of-day complexity.
A third problem is ignoring timezone conversion when birthdays are stored with UTC timestamps or mixed regional rules.
Finally, if the data truly represents a birthday, storing it as DateTime instead of a date-only type creates avoidable ambiguity.
Summary
- Correct age calculation compares the current date to the birth month and day.
- In C#, subtract the birth year and reduce by one if the birthday has not happened yet this year.
- Do not approximate age by dividing elapsed days by
365. - Prefer
DateTime.Todayfor calendar-based age checks. - If possible, model birthdays as date-only values instead of full timestamps.

