function setValue() {
var startTime = document.getElementById('ToilA');
var endTime = document.getElementById('EndHours'); startTime = startTime.value.split(":");
var startHour = parseInt(startTime[0], 10);
var startMinutes = parseInt(startTime[1], 10);
endTime = endTime.value.split(":");
var endHour = parseInt(endTime[0], 10);
var endMinutes = parseInt(endTime[1], 10);
//var hours, minutes;
var today = new Date();
var time1 = new Date(2000, 01, 01, startHour, startMinutes, 0);
var time2 = new Date(2000, 01, 01, endHour, endMinutes, 0); var milliSecs = (time2 - time1);
msSecs = (1000);
msMins = (msSecs * 60);
msHours = (msMins * 60);
numHours = Math.floor(milliSecs/msHours);
numMins = Math.floor((milliSecs - (numHours * msHours)) / msMins);
numSecs = Math.floor((milliSecs - (numHours * msHours) - (numMins * msMins))/ msSecs); numSecs = "0" + numSecs;
numMins = "0" + numMins;
DateCalc = (numHours + ":" + numMins);
document.getElementById('CalculateHours').value = DateCalc; }
我对这段代码还有一个问题,当分钟部分超过 10 时,有一个前导 0。
所以它返回类似 11:013 而不是 11:13 的东西,这可以用数学来解决,还是 if 语句可以解决这个问题?就像如果项目数> 2,删除第一项?
我打算在提交表单时执行一个 PHP 脚本来删除它,但它看起来不太好。