Manage Learn to apply best practices and optimize your operations.

Age calculation made easy

I was doing some web dev where I needed to calculated the age of a person. I did search the web for a coded solution but came across many convoluted ways involving reams of script and complicated calculations using days, months and years allowing for leap years etc. I thought about what was needed and came up with this very simple algorithm. It assumes there are three text fields on the form named Day, Month, Year. In pseudo (human!) code it goes like this:

Initial age = Year today - Year entered.
if the entered Month > the Month today then age -1 (ie
Birthday not arrived yet!)

and also

if the entered Month = the Month of today but the Day entered
is > the Day today then age -1.

This translates in Notes formula as (for example in a button):

age := @year(@today) - @texttonumber(Year) ;
@if(@texttonumber(Month) > @month(@today) |
(@texttonumber(Month) = @month(@today) & @texttonumber(Day) >
@day(@today) ) ;
@set("age"; age-1);
@prompt([ok];""; @text(age) )

Also, for the web in JavaScript:

doc = document.forms[0];

today = new Date();
age = today.getFullYear() - Number(doc.Year.value);

if (Number(doc.Month.value) > today.getMonth()+1 ) {
age = age -1;
if ( Number(doc.Month.value) == today.getMonth()+1 &&
Number(doc.Day.value) > today.getDate() ) {
age = age -1;
alert (String(age));

Dig Deeper on Domino Resources - Part 2

  • Favorite iSeries cheat sheets

    Here you'll find a collection of valuable cheat sheets gathered from across the iSeries/ community. These cheat ...