# Calculate Age in Months and Age in Years:Months

**URL:** <https://forums.knack.com/t/calculate-age-in-months-and-age-in-years-months/8383>\
**Category:** Get Answers\
**Created:** [July 21, 2020, 7:16pm UTC](https://forums.knack.com/t/calculate-age-in-months-and-age-in-years-months/8383 "2020-07-21T19:16:04Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![CraigWolfe70978](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@CraigWolfe70978](https://forums.knack.com/u/CraigWolfe70978)\
**Post date:** [July 21, 2020, 7:16pm UTC](https://forums.knack.com/t/calculate-age-in-months-and-age-in-years-months/8383/1 "2020-07-21T19:16:04Z")

</div>

For a project I needed to figure out how old someone was when they took a test. In some contexts they wanted that in months, but in others they needed Years and months expressed as Years:Months. Those who have struggled with this realize there are several edge cases that add a lot of complexity since what we are really doing is extracting the numeric value of the year, month and day and then performing calculation on those. I've seen attempts to just get the number of days difference between two dates and then do division and rounding, but that just doesn't work.

It is pretty straightforward when each part of the end date - dd, mm and yyyy - are greater than the start date. Since this is about age the end date is always after the start date. However there are several conditions where this breaks down.

- The day of the month for the end date is before the start date
- The month of the year for the end date is before the start date
- Both of those conditions are true.
- The year of the start date is the same years as the end date (ie. less than a year has passed.)

I started basic, ran into all of those edge cases and came up with this solution. It has a lot of moving pieces but seems to cover all the edge cases.&nbsp;

Note: due to the complexity of this application I'm actually pulling the dates through connector fields. If you do this you will have to edit those field names if you copy these fields to another object. It will replace then with numerical field references during the copy. You should be good if you are using actual date fields within the object.

**Fields**

- Date of Birth - date
- Date Taken - date&nbsp;
- Month Checker - equation
- Day Checker - equation
- Age Years base - equation
- Age Years - 1 - equation
- Age Years - number (conditional)
- Age Months base - equation
- Age Months - 1 - equation
- Age Months - number (conditional)
- Age in Months - equation
- Age in Months:Years - Text Formula

**Month Checker Equation (is the end month before or after start month)**  
(getDateMonth({Date Taken.Date})-getDateMonth({Child.Date of Birth})

**Day Checker Equation (is the end day before or after start day)**  
getDateDayOfMonth({Date Taken.Date}) - getDateDayOfMonth({Child.Date of Birth})

**Age Years Base Equation**  
getDateYear({Date Taken.Date}) - getDateYear({Child.Date of Birth})

**Age Years - 1 Equation**  
{Age Years base}-1

**Age Years Conditional Logic**

_#1 - Same month for both but the end day is before start day_  
Month Checker is 0  
Day Checker lower than 0  
Set to a record value Age Years -1  
_#2 - Same month for both but the end day is on or after start day_  
Month Checker is 0  
Day Checker higher than -1  
Set to a record value Age Years Base  
_#3- End Month is after start month_  
Month Checker higher than 0  
Set to a record value Age Years Base  
_#4 - End month is before start month_  
Month Checker lower than 0  
Set to a record value Age Years -1

**Age Months base Equation**  
_If end month is the same or after start month just get the difference, otherwise add 12_

(getDateMonth({Date Taken.Date})-getDateMonth({Child.Date of Birth})\>-1 ? getDateMonth({Date Taken.Date})-getDateMonth({Child.Date of Birth}) : (getDateMonth({Date Taken.Date})-getDateMonth({Child.Date of Birth})+ 12

**Age Months - 1 Equation**  
{Age Months base} -1

**Age Months - Conditional Logic**  
_#1 - The same month but the end day is before the start day_  
Day Checker lower than 0  
Age Months -1 is -1  
Set to a custom value 11  
_#2 - End day is the same or after start day_  
Day Checker higher than -1  
Set to a record value Age Months base  
#3 -_&nbsp;End day is the before start day_  
Day Checker lower than 0  
Set to a record value Age Months - 1

**Age in Months Equation**  
{Age Years}\*12+{Age Months}

**Age in Months:Years Text Formula**  
{Age Years}:{Age Months}

Hope this helps someone else and if anyone can simplify please comment, but be sure to really test all the edge cases before you do. I got burned on several previous versions that I thought worked.

---

<div class="post-metadata">

**Author:** ![CraigWolfe70978](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@CraigWolfe70978](https://forums.knack.com/u/CraigWolfe70978)\
**Post date:** [July 23, 2020, 9:52pm UTC](https://forums.knack.com/t/calculate-age-in-months-and-age-in-years-months/8383/2 "2020-07-23T21:52:15Z")

</div>

One key thing to remember when creating condition logic is be sure to have the specific rules at the top followed by the general rules.&nbsp;

---

<div class="post-metadata">

**Author:** ![MichaelSha95380](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@MichaelSha95380](https://forums.knack.com/u/MichaelSha95380)\
**Post date:** [November 5, 2022, 3:25pm UTC](https://forums.knack.com/t/calculate-age-in-months-and-age-in-years-months/8383/3 "2022-11-05T15:25:14Z")

</div>

Just reading this having had a working age calculator forumla in my Knack for years, that has just suddenly stopped working in the past week or so.

The current formula we’ve been using is:

{Date of birth} ? currentTime(()- {Date of birth} : {Date of birth} :

Why is that suddenly now returning a “O” response everywhere?

Thanks

Michael

---

<div class="post-metadata">

**Author:** ![Kat](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/kat/32/2834_2.png) [@Kat](https://forums.knack.com/u/Kat)\
**Post date:** [November 7, 2022, 12:42pm UTC](https://forums.knack.com/t/calculate-age-in-months-and-age-in-years-months/8383/4 "2022-11-07T12:42:10Z")

</div>

Hi Michael, if you haven’t already done so, could you please contact us at [support@knack.com](mailto:support@knack.com) so that we could investigate this? Please make sure to include a link to the formula and the page with the calculator. Thanks!

---

<div class="post-metadata">

**Author:** ![DanB](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@DanB](https://forums.knack.com/u/DanB)\
**Post date:** [December 3, 2022, 4:51pm UTC](https://forums.knack.com/t/calculate-age-in-months-and-age-in-years-months/8383/5 "2022-12-03T16:51:20Z")

</div>

I also have a problem with Age calculation. First post - newbie - so forgive if I am missing something obvious.

I have a table “Clients” where the Age of the client is important. I just happened to notice that many of the Age values are incorrect. Of the first 24 tested 12 are incorrect. With the Age 1 year greater than it should be.

Am I the only person experiencing this? I followed the age calculation example in the Builder’s Guide.

If I create another field and change the date equation to include 1 decimal - you can see that the Age is incorrect if the age decimal is .5 or greater.

I am using the equation formula: {Date Of Birth} \> -2208988800 & {Date Of Birth} != 0 ? currentTime() - {Date Of Birth} : 0 If I use the formula: currentTime() - {Birthday} (i.e. NOT allowing for dates older than 1970) - results are the same. Round down seems to have no effect with either formula. Which makes sense, as round down would not affect .5 and above.

I could correct this if I had a FLOOR operator. I can fix most issues if I use the CEILING operator  
(CEIL) - 1 — problem is - if the decimal value is .0 - then the CEILING operator does not work - or rather does not fix the issue - it creates an issue.

So with no fix, 50% of ages are incorrect. With the CEIL-1 logic I still have close to 10% error.  
The Age could be shown with a decimal and always be “correct” - I consider this unacceptable.

What am I missing?

---

<div class="post-metadata">

**Author:** ![Ally](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/ally/32/2456_2.png) [@Ally](https://forums.knack.com/u/Ally)\
**Post date:** [December 4, 2022, 7:37am UTC](https://forums.knack.com/t/calculate-age-in-months-and-age-in-years-months/8383/6 "2022-12-04T07:37:57Z")

</div>

Hi DanB

Please make sure to send through a support ticket (by sending an e-mail to [support@knack.com](mailto:support@knack.com) or using our in-app messenger) so that we can check and see what is going on!  
Please make sure to include the link to your formula.

Thanks!

---

<div class="post-metadata">

**Author:** ![DanB](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@DanB](https://forums.knack.com/u/DanB)\
**Post date:** [December 4, 2022, 2:42pm UTC](https://forums.knack.com/t/calculate-age-in-months-and-age-in-years-months/8383/7 "2022-12-04T14:42:03Z")

</div>

Hi Allyson,

Same requested on Nov. 10, 2022 Ticket #166340. I showed examples and sent link.

Will do again.

---

<div class="post-metadata">

**Author:** ![Ally](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/ally/32/2456_2.png) [@Ally](https://forums.knack.com/u/Ally)\
**Post date:** [December 5, 2022, 7:28am UTC](https://forums.knack.com/t/calculate-age-in-months-and-age-in-years-months/8383/8 "2022-12-05T07:28:44Z")

</div>

Hi DanB, thanks I have just had a look and can confirm that this ticket has been sent to our Engineering team!

---

<div class="post-metadata">

**Author:** ![DanB](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@DanB](https://forums.knack.com/u/DanB)\
**Post date:** [December 5, 2022, 11:46am UTC](https://forums.knack.com/t/calculate-age-in-months-and-age-in-years-months/8383/9 "2022-12-05T11:46:54Z")

</div>

Thanks Allyson!

---

<div class="post-metadata">

**Author:** ![hazel](https://avatars.discourse-cdn.com/v4/letter/h/e56c9b/32.png) [@hazel](https://forums.knack.com/u/hazel)\
**Post date:** [February 2, 2023, 1:31pm UTC](https://forums.knack.com/t/calculate-age-in-months-and-age-in-years-months/8383/10 "2023-02-02T13:31:21Z")

</div>

Newbie to knack as well. Just tried the formula from help docs =\> currentTime() - {Birthday}. However I have solved it.

This does not round down correctly because the help document has decimals as none. Choosing decimals as “period” or “comma” with number of decimals as 0 works.

My conclusion is that with decimals “none” knack system somewhere rounds value to whole values before applying to the rounding rule required.
