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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help me with this code

    I'm posting my code in hopes that someone can take it, alter it slightly and make it work. Thanks.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    <META NAME="Generator" CONTENT="EditPlus">
    <META NAME="Author" CONTENT="">
    <META NAME="Keywords" CONTENT="">
    <META NAME="Description" CONTENT="">
    </script>
    <script language="JavaScript"><!--
    function y2k(number) { return (number < 1000) ? number + 1900 : number; }
    
    function daysElapsed(values1,time1,values2,time2) 
    {
    	var date1array = values1.split('-'); 
    	var newdate1 = new Date(date1array[1]+'/'+date1array[0]+'/'+date1array[2]);
    	
    	var date3array = time1.split(':'); 
    	var newdate3 = new Date(date3array[1]+':'+date3array[0]+':'+date3array[2]);
    
    	var date2array = values2.split('-'); 
    	var newdate2 = new Date(date2array[1]+'/'+date2array[0]+'/'+date2array[2]);
    	
    	var date4array = time2.split(':'); 
    	var newdate4 = new Date(date4array[1]+':'+date4array[0]+':'+date4array[2]);
    
    	alert(newdate1);
    	alert(newdate3);
    	alert(newdate2);
    	alert(newdate4);
    
    	var date1 = new Date(newdate1+'/'+newdate3);
    	var date2 = new Date(newdate2+'/'+newdate4);
    	
    
        var difference =
            Date.UTC(y2k(date2.getYear()),date2.getMonth(),date2.getDate(),date2.getHours(),date2.getMinutes(),date2.getSeconds())
          - Date.UTC(y2k(date1.getYear()),date1.getMonth(),date1.getDate(),date1.getHours(),date1.getMinutes(),date1.getSeconds())
    	  
    
    	return difference/1000/60/60;
    	
    	//var rawHour = difference/1000/60/60; //Actual time in hours. Like 2.5 for 2.30
    	//var nbHour = Math.floor(rawHour); //Put the hour to a whole number. Like 2
    	//var rawMinute = (rawHour - nbHour) * 60; //Take the Raw minutes and convert them to actual minutes. Like .5 to .30
    	//var nbMinute = rawMinute/100; //Round of Minutes to a whole number. Like 0.03
    	//var rawSeconds = (rawMinute - nbMinute) * 60; 
    	//var nbSeconds = Math.floor(rawSeconds);
    	
    	//return nbHour + '.' + nbMinute + '.' + nbSeconds;
    	}
    
    
    //--></script>
    </HEAD>
    
    <BODY>
    <form name="form1" >
    <tr>
                    <td><strong>Start Date:</strong></td>
                    <td><input name="date" type="text" class="tarea" id="date" value="yyyy-mm-dd" size="15">
                      <input name="datetime" type="text" id="datetime" value="hh:mm:ss" size="15"></td>
                  </tr>
                  <tr>
                    <td><strong>End Date:</strong> </td>
                    <td><input name="end" type="text" class="tarea" id="end" value="yyyy-mm-dd" size="15">
                      <input name="endtime" type="text" id="endtime" value="hh:mm:ss" size="15"></td>
                  </tr>
                  <tr>
                    <td><strong>Duration:</strong></td>'
                    <td><input name="duration" type="text" class="tarea" id="duration" onClick="this.value = daysElapsed( document.form1.date.value , document.form1.datetime.value , document.form1.end.value , document.form1.endtime.value );" size="30"></td>
                  </tr>
    </form>
    </BODY>
    </HTML>

  • #2
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    There was a lot wrong with your script
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    	<head>
    		<title>46980</title>
    		<script type="text/javaScript">
    			function diff(startdate,starttime,enddate,endtime)
    			{
    				startdate = startdate.split('-');
    				starttime = starttime.split(':');
    				var start = new Date(startdate[0],startdate[1] - 1,startdate[2],starttime[0],starttime[1],starttime[2]);
    
    				enddate = enddate.split('-');
    				endtime = endtime.split(':');
    				var end = new Date(enddate[0],enddate[1] - 1,enddate[2],endtime[0],endtime[1],endtime[2]);
    
    				var mshour = 1000 * 60 * 60;
    
    				return Math.round((end.getTime() - start.getTime()) / mshour) + " hours";
    			}
    		</script>
    	</head>
    	<body>
    		<form name="form1">
    			<fieldset>
    				<label for="startdate">Start date</label>
    				<input id="startdate" name="startdate" type="text" value="2003-04-03" size="10">
    
    				<label for="starttime">Start time</label>
    				<input id="starttime" name="starttime" type="text" value="12:31:05" size="10">
    
    				<label for="enddate">End date</label>
    				<input id="enddate" name="enddate" type="text" value="2003-04-04" size="10">
    
    				<label for="endtime">End time</label>
    				<input id="endtime" name="endtime" type="text" value="09:52:01" size="10">
    
    				<label for="duration">Duration</label>
    				<input id="duration" name="duration" type="text" onclick="this.value = diff( document.getElementById('startdate').value , document.getElementById('starttime').value , document.getElementById('enddate').value , document.getElementById('endtime').value );" size="10">
    			</fieldset>
    		</form>
    	</body>
    </html>


  •  

    Posting Permissions

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