...

View Full Version : calculating date -> integer



crmpicco
07-05-2005, 12:32 PM
20051001
20030929
________
20072

20051001
20031002
________
19999

20051001
20040509
________
10492

I am looking for a way to determine if a DOB (or Integer) is more than 2 years.

The above calculations give me values that dont help.

All Dates are YYYYMMDD to help calculations.



<script>
/////////////////////////////////////////////////
// CHECK THE RETURN DATE OF THE JOURNEY //
// AGAINST THE DATE OF BIRTH OF THE PASSENGER //
/////////////////////////////////////////////////
// DESCRIPTION: //
// TO DETERMINE IF THE PASSENGER CHANGES AGE //
// DURING THE JOURNEY (APPLICABLE to CHD & INF)//
/////////////////////////////////////////////////
function checkRetDate(obj)
{

yyyy = passtocheckRetDate.slice(4,8) // YYYY (2004)
mm = passtocheckRetDate.slice(2,4) // MM (02)
dd = passtocheckRetDate.slice(0,2) // DD (27)

yyyymmdd = yyyy + mm + dd; // YYYYMMDD (20040227)

retdtminusdob = eval(obj.value-yyyymmdd) // difference between the return date and the INF date of birth
alert("RETURN DATE - INFANT DOB = \n" + retdtminusdob);

if(retdtminusdob < 20000)
{
alert("This Infant will turn 2 during this journey\nThis passenger must be classed as a Child and not an Infant");
submit_form=false;
}
}
</script>

martin_narg
07-05-2005, 12:45 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script>
/* Extension to Date object by glenngv
* (http://www.codingforums.com/member....tposter&t=62011)
*/
Date.prototype.getDiff = function(date, interval){
if (typeof date == "string"){
date = new Date(date);
}
if (isNaN(date) || !(date instanceof Date)){
return NaN;
}
if (typeof interval == "undefined") interval = "ms";
var diff = this - date;
switch(interval.toLowerCase()){
case "s":
diff = diff/1000; break;
case "n":
diff = diff/(1000*60); break;
case "h":
diff = diff/(1000*60*60); break;
case "d":
diff = diff/(1000*60*60*24); break;
case "m":
diff = diff/(1000*60*60*24*30); break;
case "y":
diff = diff/(1000*60*60*24*365); break;
default:
;
}
return Math.floor(diff);
}

function checkBirthday(obj) {
if( !/^(\d{4})(\d{2})(\d{2})$/.test(obj.value) ) {
alert( "Invalid date supplied - must be format DD/MM/YYYY" );
obj.focus();
return false;
}

var d = new Date();
var d2 = new Date(RegExp.$1, RegExp.$2, RegExp.$3);

var diff = d.getDiff(d2, "y")
if( isNaN(diff) ) {
alert( "Invalid date supplied" );
return false;
}
else if( diff < 2 ) {
alert( "Difference less than 2 years" );
return false;
}

return true;
}
</script>
</head>

<body>
<form name="myForm" onsubmit="return checkBirthday(this.birthday);">
<input type="text" name="birthday"><br>
<input type="submit" value="check date">
</form>
</body>
</html>


This code is only a minor change from this code:
http://www.codingforums.com/showthread.php?t=62027

To a question that you have a large amount of threads on.

Please keep all your posts regarding the same questions in the same original thread.

The code which you are being provided by myself and the other members is correct for the question asked. I would strongly suggest that you read up about JavaScript and programming in general.
JavaScript information: http://www.w3schools.com/js/default.asp
JS reference: http://www.devguru.com/Technologies/ecmascript/quickref/javascript_index.html

Please be aware that we are all willing to help, just make sure that you are focused in what you are asking for.

Hope this helps

m_n

crmpicco
07-05-2005, 01:08 PM
thanx

crmpicco
07-05-2005, 04:18 PM
for (i=0; i<document.form.nochd.value; i++)
{

... code .....

forename = document.form.clname1.value
surname = document.form.cfname1.value

forename2 = document.form.clname2.value
surname2 = document.form.cfname2.value

}


Is there a way i can make this code


forename = document.form.clname1.value
surname = document.form.cfname1.value

forename2 = document.form.clname2.value
surname2 = document.form.cfname2.value


more dynamic?

The real names of the fields are clname1, cfname1, clname2 etc....

martin_narg
07-05-2005, 04:26 PM
you can use something like this:


for( var i=1; i<10; i++ ) {
window["forename"+i] = document.form.elements["cfname"+i].value;
}


hope this helps

m_n

crmpicco
07-06-2005, 09:36 AM
This script only takes into consideration the YEAR when calculating the AGE. It does not seem to
bother with the Date or Month. Is there something i can change?

29-JUN-1993 goes into the diff < 12 statement, whereas it should go into the diff > 12 statement.



///////////////////////////////////////
// CHILD Birthday Checker //
///////////////////////////////////////
// DESCRIPTION: //
// CHECK IF THE DECLARED CHILD DOB //
// IS LESS THAN 12 YEARS OLD //
///////////////////////////////////////
function checkChdBirthday(obj)
{
alert("function = checkChdBirthday");
df = document.form;
retdate = df.retdt.value;


for (i=0; i<document.form.nochd.value; i++)
{
if( !/^(\d{1,2})(\d{1,2})(\d{4})$/.test(obj.value) ) {
alert( "Invalid date supplied - must be format DDMMYYYY" );
return;
}

var d = new Date();
var d2 = new Date(RegExp.$3, RegExp.$2, RegExp.$1);
nochd = document.form.nochd.value
noinf = document.form.noinf.value

var diff = d.getDiff(d2, "y")

if( isNaN(diff) ) {
alert( "Invalid date supplied" );
}
else if( diff < 12 ) {
alert( "This IS an CHILD \n they are younger than 12" );
child = true;
submit_form=true;
under12counter = under12counter + 1;
passtocheckRetDate = obj.value;
checkRetDate(document.form.retdt);

}
else if( diff > 12 ) {
alert( "This is NOT an CHILD \n they are older than 12" );
child = false;

submit_form=false;
chdcounter = chdcounter + 1; // amount of declared children that are found to be under 12
childno = i+1

forename = document.form.cfname1.value
surname = document.form.clname1.value


if(chdcounter==2)
{
forename2 = document.form.clname2.value
surname2 = document.form.cfname2.value
}

if(chdcounter==3)
{
forename3 = document.form.clname3.value
surname3 = document.form.cfname3.value
}

}
}
inf_ok=false;
}

crmpicco
07-07-2005, 10:52 AM
///////////////////////////////////////
// CHILD Birthday Checker //
///////////////////////////////////////
// DESCRIPTION: //
// CHECK IF THE DECLARED CHILD DOB //
// IS LESS THAN 12 YEARS OLD //
///////////////////////////////////////
function checkChdBirthday(obj)
{
df = document.form;
retdate = df.retdt.value; // Return Date of Journey in YYYYMMDD format
if(document.form.nochd.value==1)
{
nochdvalue=document.form.nochd.value
}
else
{
nochdvalue=document.form.nochd.value-1
}

for (i=0; i<nochdvalue; i++) // loop through the number of children
{
number=number+1;
if( !/^(\d{1,2})(\d{1,2})(\d{4})$/.test(obj.value) )
{
alert( "Invalid date supplied - must be format DDMMYYYY" );
return;
}

// pass Infant DOB to check_date to check for invalid dates
// e.g. 30022002
check_date(obj);

var d = new Date();
var d2 = new Date(RegExp.$3, RegExp.$2, RegExp.$1);
nochd = document.form.nochd.value
noinf = document.form.noinf.value

var diff = d.getDiff(d2, "y")
if( isNaN(diff) )
{
alert( "Invalid date supplied" );
}
else if( diff < 12 ) {
//alert( "This IS an CHILD \n they are younger than 12" );
child = true;
submit_form=true;
under12counter = under12counter + 1;
passtocheckRetDate = obj.value;
checkRetDate(document.form.retdt);

}
else if( diff > 12 ) {
//alert( "This is NOT an CHILD\nthey are older than 12" );
child = false;
submit_form=false;
chdcounter = chdcounter + 1; // amount of declared children that are found to be under 12
childno = i+1

forename = document.form.cfname1.value
surname = document.form.clname1.value

if(chdcounter==2)
{
if(document.form.cfname2.value!="")
{
forename2 = document.form.cfname2.value
surname2 = document.form.clname2.value
}
}

if(chdcounter==3)
{
if(document.form.cfname3.value!="")
{
forename3 = document.form.cfname3.value
surname3 = document.form.clname3.value
}
}
}
}
inf_ok=false;
}


is there a way to change the above code to take into consideration
the date and the month when finding the difference in the two dates.

Something in the RegExp function?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum