Calculate Age in Months and Age in Years:Months

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. 

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 
  • 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 - 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.

2 Likes

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. 

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

Hi Michael, if you haven’t already done so, could you please contact us at 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!

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?

Hi DanB

Please make sure to send through a support ticket (by sending an e-mail to 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!

Hi Allyson,

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

Will do again.

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

Thanks Allyson!

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.

2 Likes