-3
import datetime

start_time = datetime.datetime.now()

end_time = datetime.datetime.now()
print (end_time - start_time)

I try to to use datetime to get the execution time.

If it took almost 11 hours, it will show like 11:07:13.215032

If more than 24 hours, how to show the time?

e.g. 35 hours 11minutes 37 seconds

1) 35:11:37
2) 1:11:11:37

Which one will be shown?

  • 1
    Why don't you try it yourself by assigning a "fake" date to `start_time`? – DeepSpace Feb 28 '17 at 09:04
  • It will show 2 since it went to days. – Taku Feb 28 '17 at 09:05
  • Why don't you try? datetime can receive some arguments, check the docs: https://docs.python.org/2/library/datetime.html Remember to select the version of Python you are using in the upper corner, or tell me your version and ill provide you the way to check it by yourself – Adirio Feb 28 '17 at 09:05
  • 1
    Even simpler test: `from datetime import timedelta; print timedelta(days=2, hours=4)` – deceze Feb 28 '17 at 09:06
  • You're trying to pring a `timedelta`-object, this answer might help you: http://stackoverflow.com/a/539360/2756793 – Khris Feb 28 '17 at 09:11

1 Answers1

0

Just give it a start_time

import datetime
start_time = datetime.datetime.strptime('1997-01-01 00:00:00', '%Y-%m-%d %H:%M:%S')
end_time = datetime.datetime.now()
print (end_time - start_time)

Output:

7363 days, 17:06:57.965556
McGrady
  • 10,869
  • 13
  • 47
  • 69