Oh hey, I actually faced something really similar a while back when I was working on a little time tracking project for train arrivals and departures. Your current setup works fine when the arrival is after the scheduled time, which usually means there’s a delay. But yeah, when the train shows up earlier, the logic kinda breaks, right?
So what I did back then was add a small tweak to handle both late and early arrivals without messing up the output. Here's a slightly adjusted version of your function that might help:
functiontomdiff(t1, t2) { var m1 = hour2mins(t1); var m2 = hour2mins(t2); var diff = m2 - m1; if (diff < -720) { diff += 1440; } else if (diff > 720) { diff -= 1440; } return mins2hour(diff); }
This way, the function should return a positive value if the train is late and a negative one if it arrives early, which makes it easier to understand at a glance.
Also, just as a side note, when I was testing time inputs and checking if the differences were calculated correctly, I found Stundenrechner really useful. It’s basically a simple tool to calculate time differences and delays between two times, and honestly, it helped me catch a few bugs in my own setup.
Regards
Muhamamd Shozab