Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,097
    Thanks
    15
    Thanked 1 Time in 1 Post

    Angry calculating date -> integer

    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.

    Code:
    <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>

  • #2
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Code:
    <!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/...ipt_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
    Last edited by martin_narg; 07-05-2005 at 01:50 PM.
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #3
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,097
    Thanks
    15
    Thanked 1 Time in 1 Post
    thanx

  • #4
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,097
    Thanks
    15
    Thanked 1 Time in 1 Post
    Code:
    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
    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....

  • #5
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    you can use something like this:
    Code:
    for( var i=1; i<10; i++ ) {
    	window["forename"+i] = document.form.elements["cfname"+i].value;
    }
    hope this helps

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #6
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,097
    Thanks
    15
    Thanked 1 Time in 1 Post

    Exclamation

    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.

    Code:
    ///////////////////////////////////////
    // 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;	
    }

  • #7
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,097
    Thanks
    15
    Thanked 1 Time in 1 Post
    Code:
    ///////////////////////////////////////
    // 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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •