从另一个javascript中减去一次时的前导零

时间:2010-07-07 19:54:27

标签: javascript datetime math parsing modulus

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:13而不是11:13这样的东西,这可以用数学修复还是if语句修复这个?喜欢项目数量> 2,删除第一项?

我提交的PHP脚本只是在提交表单时将其删除,但看起来不太好。

2 个答案:

答案 0 :(得分:3)

numMins = "0" + numMins更改为

if (numMins < 10)
    numMins = "0" + numMins;

答案 1 :(得分:0)

if (numSecs < 10)
      numSecs = "0" + numSecs;
      if  (numMins < 10)
      numMins = "0" + numMins;