How to get hour format in two digit (00.00.00) in python?

Below is the sample code I am using inside my one of the application. I want Default hour format as 00 (e.g. 02:30:35) and here I am getting as 0:00:03.011445 so I am using strftime but I am getting another exception there. Please guide on it.

from datetime import datetime
from datetime import time
from time import strftime
import time

start = datetime.now()
time.sleep(3)
end = datetime.now()

print(start)
print(end)
dt = (end - start)
print('Defual Time:', dt)

print('New Format DateTime', dt.strftime("%m/%d/%y %H:%M:%S"))

I am getting below output.

2020-10-17 19:15:36.831928
2020-10-17 19:15:39.843373
Defualt Time: 0:00:03.011445
Traceback (most recent call last):
  File "D:/New folder/IoT2.py", line 30, in <module>
    print('New Format DateTime', dt.strftime("%m/%d/%y %H:%M:%S"))
AttributeError: 'datetime.timedelta' object has no attribute 'strftime'


Read more here: https://stackoverflow.com/questions/64403223/how-to-get-hour-format-in-two-digit-00-00-00-in-python

Content Attribution

This content was originally published by Amaze_Rock at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: