Calculate a Person's Age from a Birth Date Field

Author: Tripp W Black

Created: 03/07/2001 at 08:24 PM

 

Category:
Notes Developer Tips
LotusScript

Age calculation made easy

I was doing some Web development where I needed to
calculate 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 (i.e.
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.

CODE
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);
NULL);
@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;
}
else
if ( Number(doc.Month.value) == today.getMonth()+1 &&
Number(doc.Day.value) > today.getDate() ) {
age = age -1;
}
alert (String(age));

Submitted by Jean-Marc Elliott to SearchDomino.com as a
Lotus411 Developer Tip


previous page