add input in time format

Asked

Viewed 194 times

0

I have 3 input the first receives start time the second departure time and the third 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;
} 

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

Thank you!

1 answer

3

This happens because the account is being made backwards.

Should be segsai (hora saída) which is the higher value, less segent (hora entrada) which will be the lower 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.