Click to See Complete Forum and Search --> : date calculation in forms


janix
08-18-2003, 07:26 AM
Is it possible to input a date in a form that uses the system date to calculate a persons age?

If that is possible, how can I set an alert if the age does not fall between an upper and lower age limit eg 18 to 30?

I can sort out the input for the date and I can automatically set 'today's date' in another field, but thereafter it gets a bit foggy - hope someone can help please.

requestcode
08-18-2003, 08:09 AM
Here is a link to a script that calculates your age in different units. Maybe it will help:
http://www.javascriptkit.com/script/script2/agefind.shtml

janix
08-19-2003, 02:33 AM
Thanks, that has helped a little but I'm still not sure how to set up an alert if someone falls outside the age range.

Gollum
08-19-2003, 03:56 AM
well if...

var now = new Date();
// and
var birthday = new Date(y,m,d);
// then
var age = now.getYear() - birthday.getYear();
var tmp = new Date(now.getYear(),m,d);
if ( tmp.getTime() > now.getTime() ) age--;
// so
if ( (age < 18) || (age >= 31) ) alert("You're either too old or too young to enjoy this holiday package.");