I need to subtract timedeltas in python and return the number in just a second form.
I currently have:
from datetime import datetime, timedelta
q1_start_time = datetime.now()
q1_end_time = datetime.now()
total_time = q1_end_time - q1_start_time
print(total_time)
This returns:
0:00:01.549000
How can I get it to just return 1, or best case scenario it would return 2 ( rounding up ). I don't need 0:00:01 just need 1.
Use:
print(total_time.total_seconds())
It's available for objects of type datetime.timedelta
since Python 2.7.
math.ceil()
is used to round floats to the next greater/equal integer number. Other rounding functions are math.floor()
, and round()
. If it's just about beautifying the printed representation of the number, you could also use Python's string formatting functionality, eg,
print "elapsed: %.2f seconds" % (t_end-t_start).total_seconds()
For reference:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.