-1

How can I subtract end - start to get hours minutes and seconds of time completion in Python?

I have some pseudocode here, I want to convert the print statement to what I said above.

start = time.asctime(time.localtime(time.time()))

< some code here>

end = time.asctime(time.localtime(time.time()))

print(end - start)

Eisen
  • 1,697
  • 9
  • 27

1 Answers1

1

a solution using datetime

You can use the datetime module in Python to subtract two datetime objects and obtain a timedelta object that represents the duration between the two times. The timedelta object can be further used to extract hours, minutes, and seconds by accessing its attributes total_seconds(), seconds, minutes, hours, and days. Here is an example:

import datetime  

start = datetime.datetime.now()
end = datetime.datetime.now()

duration = end - start

hours, remainder = divmod(duration.total_seconds(), 3600)
minutes, seconds = divmod(remainder, 60)
Conic
  • 998
  • 1
  • 11
  • 26