add input in time format

0

I have 3 input the first receives start time the second hour of exit and the third the subtraction between them.

function calcular(){
    var segent = parseInt(document.getElementById('segent').value, 10);
    var segsai = parseInt(document.getElementById('segsai').value, 10);
    document.getElementById('resultseg').value = segent - segsai;
} 

I need to calculate input-output, but with this code it returns odd values like -2, 0, -8 ...

Thank you!

    
asked by anonymous 29.07.2017 / 02:51

1 answer

3

This is because the account is being reversed.

It should be segsai (hora saída) which is the largest value, less segent (hora entrada) which will be the smallest value.

var calc = document.getElementById('calcular');
calc.onclick = function(){
    var segent = parseInt(document.getElementById('segent').value, 10);
    var segsai = parseInt(document.getElementById('segsai').value, 10);
    document.getElementById('resultseg').value = segsai - segent;
}
<input id="segent" value="09:00" type="time"/>
<input id="segsai" value="17:00" type="time"/>
<input id="resultseg"/>
<button id="calcular">calcular</button>
    
29.07.2017 / 03:35