Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calculating diff between two times 2

Status
Not open for further replies.

andycape

Programmer
Aug 22, 2003
177
ZA
I want to calculate the difference between two given times. The times will be given in the format hhmm (eg : 1030).

What would be the best way if I had a start and end time to calculate the difference between the two (answer in minutes)?

I thought of something like this but its not right :

say Start = 1030
End = 1145

function Time(which)
{
vala = left(document.myform.End.value,2);
valb = right(document.myform.End.value,2);
valc = left(document.myform.Start.value,2);
vald = right(document.myform.Start.value,2);
document.getElementById("Time").innerHTML = (parseInt(vala) - parseFloat(valc))*60 +(parseInt(valb) - parseFloat(vald));
}

 

If you knnow the hours and minutes of each one, and you know that they're always in 24-hour time, and both on the same day, then convert them all to minutes to do the calculation

Code:
var startInMins = (startHours * 60) + startMins;
var endInMins = (endHours * 60) + endMins;
var diffInMins = endInMins - startInMins;
var diffHours = parseInt(diffInMins / 60);
var diffMins = diffInMins - (diffHours * 60);

So the difference in hours and minutes will be in the variables diffHours and diffMins accordingly.

Note: I haven't tested this, but it sounds right... Let me know if it doesn't work for you ;o)

Hope this helps,
Dan
 
Its not quite working, but I will carry on trying.
The times will always be on the same day, and hopefully in 24hr clock (although i need to cover the possibility that the user will not enter 24hr).

The logic of the method i was thinking is correct (it works in asp) but the left() and right() method dont seem to work in javascript, do you know what the equivalent methods are ??
 
Try this example to see how you can get an answer - of course it requires that the times are in 24 hr time (otherwise you can't know if 0330 means 0330 or 1530 to the user)... but I'll leave that validation up to you:

Code:
<html>
<head>
<script type="text/javascript">
function compareTime(startTime, endTime)
{
  var startMins = parseInt(startTime.substring(0,2)) * 60 + parseInt(startTime.substring(2,4));
  var endMins = parseInt(endTime.substring(0,2)) * 60 + parseInt(endTime.substring(2,4));
  var deltaTimeMins = endMins - startMins;
  var deltaHours = Math.floor(deltaTimeMins/60);
  var deltaMins = deltaTimeMins % 60;
  alert("The difference between " + startTime + " and " + endTime + " is " + deltaTimeMins + " minutes (" + deltaHours + "h " + deltaMins + "m)");
}
</script>
</head>
<body onload="compareTime('1200', '1420')">
</body>
</html>

Hope this helps...
Jeff
 
thanx alot that gets the right answer most of the time (if the difference between the two times is small, but if i use say 0900 and 1550 then its incorrect. I will try play with it and sort it out. thanx.
 
Interesting stuff happening there... first off here is a replacement for the main function:

Code:
<script type="text/javascript">
function compareTime(startTime, endTime)
{
  var startMins = startTime.substring(0,2) * 60 + startTime.substring(2,4) * 1;
  var endMins = endTime.substring(0,2) * 60 + endTime.substring(2,4) * 1;
  var deltaTimeMins = endMins - startMins;
  var deltaHours = Math.floor(deltaTimeMins/60);
  var deltaMins = deltaTimeMins % 60;
  alert("The difference between " + startTime + " and " + endTime + " is " + deltaTimeMins + " minutes (" + deltaHours + "h " + deltaMins + "m)");
}
</script>

Now... the change was in the first 2 variable decalrations. I was splitting the time (string) into 2 strings (of 2 characters each). Then using parseInt() to convert it into a number. It turns out that parseInt("09") returns 0. Multiplying (by a number) has the desired effect of converting a string into a number (and "09" * 1 == 9).

I guess I'll look into a more correct solution.

Jeff
 
thanx alot yes that helps,
I also saw it was returning a 0.

 
Jeff,

Here's a quick tip. When using parseInt, always follow your string with a ", 10". If you pass any numbers to parseInt that have a leading zero it treats the number as an octal. However, passing the extra parameter of 10 makes it always treat the number as a decimal number.

parseInt("09") == 0
parseInt("09", 10) == 9

-kaht

banghead.gif
 
Thanks Kaht... and take a star for being so right :)

I must have had a brain wipe this morning (or not enough caffiene maybe) - I knew about that base parameter *grrr*.

I first discovered it when reading a copy of the wonderful "Javascript: The Definitive Guide" (Edition 2?)... and annotated the page with red pen to remind me of it (the book neglected to hilight this if I recall).

Great tip for us all to remember anyway.

Jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top