Search code examples
pythonpython-3.xformattingcomparisonstring-comparison

python3 why does my comparison change the formatting?


I have 1 time that is formatted like this

start_time  01:13:05

I have a second time that looks like it

Current time  01:13:10

When I calculating the difference, it produces the correct answer but chops a zero off

code:

new_start_time = datetime.strptime(start_time, '%H:%M:%S')
new_current_time = datetime.strptime(str(current_time), '%H:%M:%S')
elapsed_time = new_current_time - new_start_time

produces:

elapsed time  0:00:10

The 10 is correct but what happened to the zero? How do I hack it back on? I need it.


Solution

  • Add:

    elapsed_split = str(elapsed_time).split(":")
    elapsed_time = int(elapsed_split[0]).zfill(2) + elapsed_split[1:]