Q:

Calculating difference of two time inputs in seconds in javascript

I am trying to get the difference between two time inputs and display it as HH:mm:ss.
An input box with id="diff" was created for the display. This code works for up to 24hours difference which is all I need.  
However, when the input for seconds for either start or end time is 0 (Eg. 11:10:00 PM), the display reflects NaN. 
Anyone knows what the problem is here?

var start = document.getElementById("start").value;
var end = document.getElementById("end").value;

function diff(start, end) {
    start = start.split(":");
    end = end.split(":");
    var startDate = new Date(0, 0, 0, start[0], start[1], start[2], 0);
    var endDate = new Date(0, 0, 0, end[0], end[1], end[2], 0);

    //for calculation to work if endtime cross over next day. Eg. 11pm to 2am
    if (endDate.getTime() < startDate.getTime()) {
        endDate.setDate(endDate.getDate() + 1);
    }

    var diff = endDate.getTime() - startDate.getTime();

    var hours = Math.floor(diff / 1000 / 60 / 60);
    diff -= hours * 1000 * 60 * 60;
    var minutes = Math.floor(diff / 1000 / 60);
    diff -= minutes * 1000 * 60;
    var seconds = Math.floor(diff / 1000);


    return (hours < 9 ? "0" : "") + hours + ":" + (minutes < 9 ? "0" : "") + minutes + ":" + (seconds < 9 ? "0" : "") + seconds;

}

document.getElementById("diff").value = diff(start, end);
<input type="time" id="start" step="1" value="">
<input type="time" id="end" step="1" value="">
<input id="diff">

A:

That's because your "00" in seconds never gets passed. Only minutes and hours are passed. So, your Dates are invalid when that happens.
Just check for the last parameter of the split array and pass zero if t doesn't exist.
Try changing this:
var startDate = new Date(0, 0, 0, start[0], start[1], start[2], 0);
var endDate = new Date(0, 0, 0, end[0], end[1], end[2], 0);

to this:
var startDate = new Date(0, 0, 0, start[0], start[1], start[2] ? start[2] : 0, 0);
var endDate = new Date(0, 0, 0, end[0], end[1], end[2] ? end[2] : 0, 0);

