Akhil Kodicherla
Akhil Kodicherla

Reputation: 61

Converting 12 hr to 24 hr python shows 24:00:00 as 24:00:00 and not 00:00:00

when i used the following code. I do understand that i am adding 12 to the time given and it returns 24:00:00 but i am not able to understand how to get 00:00:00.

My timestamp has list of time values.

r = timestamp[:-2] if timestamp[-2:] == "AM" else str(int(timestamp[:2]) + 12) + timestamp[2:8] 

Upvotes: 0

Views: 166

Answers (1)

TigerhawkT3
TigerhawkT3

Reputation: 49318

Use modulo arithmetic to divide by 24 and get the remainder. Change this:

int(timestamp[:2]) + 12

to this:

(int(timestamp[:2]) + 12) % 24

Demonstration:

>>> 12 + 12
24
>>> (12 + 12) % 24
0

Upvotes: 2

Related Questions