View Full Version : Help me with this code

Nov 3rd, 2004, 03:14 AM
I'm posting my code in hopes that someone can take it, alter it slightly and make it work. Thanks.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<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]);


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(),d ate2.getSeconds())
- Date.UTC(y2k(date1.getYear()),date1.getMonth(),date1.getDate(),date1.getHours(),date1.getMinutes(),d ate1.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;


<form name="form1" >
<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>
<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>
<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>

Nov 3rd, 2004, 04:41 AM
There was a lot wrong with your script
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<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";
<form name="form1">
<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">