Results 1 to 2 of 2

Thread: Calculating a year time span

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2000
    Location
    Tennessee
    Posts
    279

    Question Calculating a year time span

    How can I check to see that a date entered by the user is no more than one year from the current date? Better yet, any number of years entered. For instance, the user would enter a 2 year time span and determine if today's date falls within a two year time span.
    A cynic knows the price of everything but the value of nothing.

  2. #2
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    Here's something I wrote a year or so ago. You can quite easily twek this to how you want it:
    Code:
    <html>
    <head>
    <title>Finds difference between two dates</title>
    </head>
    <body>
    <form name="dateforms">
    Starting date:<input name="one" type="text" value="24/02/1986">
    in the format dd/mm/yyyy.<input name="onetime" type="text" value="03:30">
    in the format hh:mm.<br>
    Ending date:<input name="two" type="text" value="13/03/2003">
    in the format dd/mm/yyyy.<input name="twotime" type="text" value="14:35">
    in the format hh:mm.<br>
    <input name="diff" type="text" value="[dates goes here]"><input name="difftime" type="text" value="[time goes here]"><br>
    <input type="button" value="Calc" onClick="workout()">
    </form>
    <script language="javascript">
    //This function can take characters from a string, used later.
    function SubChars(_string,start,end) { 
    return _string.substr(start,end-start); 
    } 
    
    function workout() {
    var initdate=window.document.dateforms.one.value;
    var enddate=window.document.dateforms.two.value;
    var inittime=window.document.dateforms.onetime.value;
    var endtime=window.document.dateforms.twotime.value;
    
    // Getting the amount of day, months and years from textboxes.
    var iday = SubChars(initdate,0,2);
    var imonth = SubChars(initdate,3,5);
    var iyear = SubChars(initdate,6,10);
    var ihour = SubChars(inittime,0,2);
    var imin = SubChars(inittime,3,5);
    var fday = SubChars(enddate,0,2);
    var fmonth = SubChars(enddate,3,5);
    var fyear = SubChars(enddate,6,10);
    var fhour = SubChars(endtime,0,2);
    var fmin = SubChars(endtime,3,5);
    
    //Calculations start here
    if (fday-iday<1) {
    	var fday = fday -1 + 31;
    	var fmonth = fmonth - 1;
    	}
    var dday = fday-iday;
    
    if (fmonth-imonth<1) {
    	var fmonth = fmonth -1 + 13; //If I don't have -1 it does not work.
    	var fyear = fyear - 1;
    	}
    var dmonth = fmonth-imonth;
    
    var dyear = fyear-iyear;
    
    if (dmonth > 11) {
    	dyear = dyear-1+2; // Here too I must subtract one first.
    	dmonth = 0;
    	}
    if (dday > 30) {
    	dmonth = dmonth -1+2; // Here I must subtract one first.
    	dday = 0;
    	}
    
    // Now the time
    if (fhour-ihour<1) {
    	dday = dday -1;
    	fhour = fhour -1+25;
    	}
    var dhour = fhour-ihour;
    if (fmin-imin<1) {
    	dhour = dhour -1;
    	fmin = fmin -1+61;
    	}
    var dmin = fmin-imin;
    
    //Now printing the results.
    var dprint = dday + "/" + dmonth + "/" + dyear;
    window.document.dateforms.diff.value = dprint;	
    //and the time...
    var dprinttime = dhour + ":" + dmin;
    window.document.dateforms.difftime.value = dprinttime;	
    }
    </script>
    </body>
    </html>
    Have I helped you? Please Rate my posts.

Posting Permissions

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



Click Here to Expand Forum to Full Width