Icemilo
Icemilo

Reputation: 53

How to convert Text 25:40:45 to 1:40:45 (24 hours format) in Python

Need some help on this. Not sure how to start. I have list of starting time which looks like enter image description here

How can I get the Clean_Start_Time in Python from Start_Time (string)? 25h is supposed to be 0100 and 24h is 0000.

Upvotes: 0

Views: 38

Answers (1)

Nathan Mills
Nathan Mills

Reputation: 2279

This should work. It splits the string on :, gets the remainder of the hour divided by twenty four then joins the string back together and returns it.

def modulo24(start_time):
    hour, minute, second = start_time.split(':')
    hour = str(int(hour) % 24)
    clean_start_time = ":".join([hour, minute, second])
    return clean_start_time

Upvotes: 1

Related Questions