Click to See Complete Forum and Search --> : Need to make sure a given date is greater than or equal to 14 days


IndyB
06-11-2003, 11:39 AM
I've got the following script which gets the user's birth date (Bdate_1) from a form, and computes the user's age. I need to add functionality to this script that also makes sure the user is greater than or equal to 14 days old. Anyone know how I might do that?

function checkBirthDate()
{
var today = new Date();
var bDate = new Date(document.frmpage.Bdate_1.value);
var minAge = document.frmpage.min_age_1.value;
var maxAge = document.frmpage.max_age_1.value;
var thisAge = ((today - bDate) / 1000 / 60 / 60 / 24 / 365.25);
if(minAge <= thisAge && thisAge <= maxAge)
{ }
else
{
alert("Your age is not between " + minAge + " and " + maxAge + ".");
frmpage.Bdate_1.value = "";
frmpage.Bdate_1.focus();
}
}

IndyB
06-11-2003, 11:57 AM
I figured it out, and here's what I did:

function checkBirthDate()
{
var today = new Date();
var bDate = new Date(document.frmpage.Bdate_1.value);
var minAge = document.frmpage.min_age_1.value;
var maxAge = document.frmpage.max_age_1.value;
var thisAge = ((today - bDate) / 1000 / 60 / 60 / 24 / 365.25);
var msPerDay = 24 * 60 * 60 * 1000 ;
var noYounger = 14;
var ageInDays = (today - bDate) / msPerDay;
if(minAge <= thisAge && thisAge <= maxAge)
{}
else
{alert("Your age is not between " + minAge + " and " + maxAge + ".");
frmpage.Bdate_1.value = "";
frmpage.Bdate_1.focus();}
if(ageInDays > noYounger)
{}
else
{alert("Your age cannot be less than 14 days.");
frmpage.Bdate_1.value = "";
frmpage.Bdate_1.focus();}
}