View Full Version : calc time elapsed?

08-21-2005, 12:12 AM
How do you calculate time elapsed between two times?

Basically, there would be separate inputs for hours and minutes. Below is an example. I would need to find out how many hours and minutes elapsed between hour1/minute1 and hour2/minute2 and show the values in the 2 inputs below them "hoursPast/minutesPast".

<input name="hours1" /> <input name="minutes1" />
<input name="hours2" /> <input name="minutes2" />

<input name="hoursPast /> <input name="minutesPast" />

I have seen similar code that will add two times, but I was unsucessful in altering it to subtract times. I'll post the code below for the adding of times, maybe someone can go off it. Any help would be appreciated. I need bascially the same setup, but calculating just the opposite.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<form onsubmit="makeTotal();return false;">
<td><input type="text"></td>
<td><input type="text"></td>
<td><input type="text"></td>
<td><input type="text"></td>
<td><input type="text"></td>
<td><input type="text"></td>
<input type="submit">

<p id="result">0

<script type="text/javascript">
var result = document.getElementById("result");
result.style.display = "none";

function makeTotal()
var list = document.getElementsByTagName("form")[0].getElementsByTagName("table")[0].getElementsByTagName("input");
var total = 0;

for(var i = 0; i < list.length; i++)
total += (i % 2) ? Number(list[i].value) : Number(list[i].value) * 60;

var time = new Date(total * 1000);

result.firstChild.nodeValue = time.getUTCHours() + ":" + time.getUTCMinutes() + ":" + time.getUTCSeconds();
result.style.display = "";

08-21-2005, 08:45 PM
bit untidy but

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"


<script language="JavaScript" type="text/javascript">

function CalTime(obj){
for (zxc0=0;zxc0<4;zxc0++){
for (zxc1=0;zxc1<4;zxc1++){
if (ips[zxc1].value==''){ return; }
if (t2<t1){ sign='-'; }

function zxcFormat(zxcv){
if (zxcv<10){ zxcv='0'+zxcv; }
return zxcv;


<span id="Time" >
<input name="hours1" onkeyup="CalTime(this);" /> <input name="minutes1" onkeyup="CalTime(this);" /><br>
<input name="hours2" onkeyup="CalTime(this);" /> <input name="minutes2" onkeyup="CalTime(this);" /><br>
<input />