JSFiddle - React, Tailwind, and code Playground
HTML
<input type="text" class="time">
<input type="text" class="time">
<input type="text" class="time">
JavaScript
function cost(run, end) {
run = (new Date()).setHours(run[0], run[1],0,0);
end = (new Date()).setHours(end[0], end[1],0,0);
run > lunch[0] && (lunch[0]=run);
end < lunch[1] && (lunch[1]=end);
if(run > lunch[1]||end < lunch[0]||run > end) return 0;
return (lunch[1] - lunch[0])/60000 ;
};
document.addEventListener("input", function(event) {
var el = event.target;
if (el.classList.contains('time')) {
var inp = document.querySelectorAll('.time');
if (inp[0].value.length != 5 || inp[1].value.length != 5) return ;
var run = inp[0].value.split(':'),
end = inp[1].value.split(':');
inp[2].value = cost()
}
});