开发者

Leading zero when subtracting one time from another javascript

function setValue() { 
var startTime = document.getElementById('ToilA'); 
var endTime = document.getElementById('EndHours'); startTime = startTime.value.split(":"); 
var startHour = parseInt(startTime[0], 10); 
var startMinutes = parse开发者_StackOverflow中文版Int(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; }

I have one more issue with this code, there is a leading 0 when the minute part is over 10.

so it return something like 11:013 rather that 11:13, can this be fixed with math or will an if statement fix this? Like if number of items > 2, remove first item?

I was going to just do a PHP script to remove this when the form is submitted, but it doesn't look good.


Change numMins = "0" + numMins to

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜