views:

33

answers:

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

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.

+2  A: 

Change numMins = "0" + numMins to

if (numMins < 10)
    numMins = "0" + numMins;
SLaks
thought i'd throw in the ternary: `numMins = (numMins < 10) ? "0" + numMins : numMins;`
Dan Heberden
Ah just saw that, thanks.
A: 
if (numSecs < 10)
      numSecs = "0" + numSecs;
      if  (numMins < 10)
      numMins = "0" + numMins;