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;
}
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;
}