24 hour time modulo

discrete mathematicsmodular arithmetic

I'm trying to work out what time it will be from midnight (00:00) and noon (12:00) given the time difference between an origin and destination of travel.

If the destination is, say, 5 hours ahead of the origin, then I could use the method found at this Math Stackexchange page:
Calculating time using modulus

So for finding time at origin when it is midnight at destination, I would do $(5+00:00) \mod 24$ to get $17:00$, or $5$pm.

But how would I do this for negative time difference?

For example, the time difference between Paris (origin) and Dhaka (destination) is $-04:00$ hours.

If I want Paris time when it's midnight in Dhaka, I'll do $(-4+00:00) \mod 24$, but I'll get the negative value of $-4$, but the answer should be $20:00$ ($8$pm).

Best Answer

If you are not keeping track of the date, you can simply use $(24+d+t)$ mod $24$ where $d$ is the time difference and $t$ is the time you are starting with. Your example would become $(24-4+00:00)$ mod $24$, which would be $(20+00:00)$ mod $24$, which would then become $20:00$.

If you are keeping track of the date, you will have to decrement it, as the extra 24 hours added to the time difference will push the date forward one day.