# Define Age based on date range

**URL:** <https://forum.ionicframework.com/t/define-age-based-on-date-range/219462>\
**Category:** Ionic Framework\
**Created:** [January 17, 2022, 12:27pm UTC](https://forum.ionicframework.com/t/define-age-based-on-date-range/219462 "2022-01-17T12:27:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![limpeh78](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/limpeh78/32/139516_2.png) [@limpeh78](https://forum.ionicframework.com/u/limpeh78)\
**Post date:** [January 17, 2022, 12:27pm UTC](https://forum.ionicframework.com/t/define-age-based-on-date-range/219462/1 "2022-01-17T12:27:56Z")

</div>

Hello World,

i got a question about date range

example : a kid is born 15th of February year 2009 Time 6am, 15/2/2009, if calculate as at today (17/1/2022), his age is still consider 12, and will be only consider age 13 after 15/2/2022 6am,

how can i make it into a calculation ?

regards

---

<div class="post-metadata">

**Author:** ![Tommertom](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@Tommertom](https://forum.ionicframework.com/u/Tommertom)\
**Post date:** [January 17, 2022, 1:26pm UTC](https://forum.ionicframework.com/t/define-age-based-on-date-range/219462/2 "2022-01-17T13:26:40Z")

</div>

Hi  
consider using date-fns

> **[Modern JavaScript Date Utility Library](https://date-fns.org/)**
>
> date-fns provides the most comprehensive yet simple and consistent toolset for manipulating JavaScript dates in a browser & Node.js.

Lot’s of complexities you don’t want to figure out yourself if it comes to dealing with dates.

---

<div class="post-metadata">

**Author:** ![limpeh78](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/limpeh78/32/139516_2.png) [@limpeh78](https://forum.ionicframework.com/u/limpeh78)\
**Post date:** [January 17, 2022, 1:33pm UTC](https://forum.ionicframework.com/t/define-age-based-on-date-range/219462/3 "2022-01-17T13:33:34Z")

</div>

hello Tommertom, happy new year 2022

thank you i will take a look

thanks

🙇‍♀️ 🙇‍♀️ 🙇‍♀️

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [January 17, 2022, 9:13pm UTC](https://forum.ionicframework.com/t/define-age-based-on-date-range/219462/4 "2022-01-17T21:13:59Z")

</div>

If you’re planning on releasing this app to a broad audience, beware that different cultures have different methods of calculating age. I would take special care to wall this calculation completely off into a single place, so that it can be modified according to locale or user preference without requiring major refactoring of the app.

---

<div class="post-metadata">

**Author:** ![limpeh78](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/limpeh78/32/139516_2.png) [@limpeh78](https://forum.ionicframework.com/u/limpeh78)\
**Post date:** [January 17, 2022, 11:43pm UTC](https://forum.ionicframework.com/t/define-age-based-on-date-range/219462/5 "2022-01-17T23:43:10Z")

</div>

Yes Sir! noted of age calculation! 🙇‍♂️

---

<div class="post-metadata">

**Author:** ![Zaid253](https://avatars.discourse-cdn.com/v4/letter/z/ccd318/32.png) [@Zaid253](https://forum.ionicframework.com/u/Zaid253)\
**Post date:** [August 19, 2026, 5:54pm UTC](https://forum.ionicframework.com/t/define-age-based-on-date-range/219462/6 "2026-08-19T17:54:33Z")

</div>

This is a great question and one that comes up frequently when building date-based features. The key challenge you’ve identified—calculating based on both date and time—is exactly what makes age calculations tricky.

For anyone building a similar feature and wanting to verify their logic or just get a quick reference, I often use this tool to cross-check the calculation: [age](https://agecalculatorsa.com/)

The `date-fns` library Tommertom recommended is indeed a solid choice for handling these complexities in JavaScript. It handles timezone issues and the exact moment-based logic you’re describing (like turning 13 at exactly 6am on the birthday).

I’d also second rapropos’s point about cultural differences. Some cultures consider a person to be 1 year old at birth, while others use the Western system where age increments on the birthday. If you’re building for a global audience, making that configurable is definitely worth the upfront effort.

For your specific use case with the 6am time constraint, the logic would be:

```auto
// Check if current date-time is past the birthday's exact time
const birthdayThisYear = new Date(birthday.getFullYear(), birthday.getMonth(), birthday.getDate(), 6, 0, 0);
const age = today.getFullYear() - birthday.getFullYear() - (today < birthdayThisYear ? 1 : 0);

```

Hope this helps anyone else who finds this thread later!
