If your current function works well for delays but not for early arrivals, the issue is that it always assumes the arrival time is later than or equal to the schedule time (or after midnight). For early arrivals, you can check if t2 < t1
and handle it differently.
For example, if you want a negative value to represent an early arrival, you could do something like:
function tomdiff(t1, t2) {
var t1 = hour2mins(t1);
var t2 = hour2mins(t2);
var diff = t2 - t1;
// If arrival is earlier, diff will be negative
if (diff < 0) {
return "-" + mins2hour(Math.abs(diff));
}
return mins2hour(diff);
}
This way, if the train arrives earlier, you'll see something like -00:15
for 15 minutes early.
If you need an easier way to handle time differences with both early and late arrivals, you can try this simple time calculator that works with both positive and negative differences.
Regards,
Shozab