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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate Time Difference

Status
Not open for further replies.

sandra45

Technical User
Apr 3, 2003
72
0
0
ID
Hi, I have a form with 3 text input boxes let's say.
Input 1 stores value of "clock in" time in the office;
Input 2 stores value of "clock out" time in the office;
Input 3 stores value of the difference between clock out and clock in time.

Problem encountered:
I can have correct differences displayed on the third input text box, however if I key in time 24:00 or 00:00, the result is wrong. For example:
Clock In: 22:30
Clock Out: 00:30
Difference: 22.0 hours

OR

Clock In: 22:30
Clock Out: 24:30
Difference: Nan Nan

What should I do with this? Thanks.

Below is the code:

function dateDiff(dateform)
{
date1 = new Date();
date2 = new Date();
diff = new Date();
//seconddate and firstdate here are using a fixed date because I only need the time (hours and minutes)
date2temp = new Date(dateform.seconddate.value + " " + dateform.ClockIn.value);
date2.setTime(date2temp.getTime());
date1temp = new Date(dateform.firstdate.value + " " + dateform.ClockOut.value);
date1.setTime(date1temp.getTime());
diff.setTime(Math.abs(date1.getTime() - date2.getTime()));
timediff = diff.getTime();
hours = Math.floor(timediff / (1000 * 60 * 60));
timediff -= hours * (1000 * 60 * 60);
mins = Math.floor(timediff / (1000 * 60));
timediff -= mins * (1000 * 60);
dateform.difference.value = hours+"."+mins;
return false;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top