'Get the time out of datetime.timedelta list
I have calculated the time difference using
datetime.strptime ('End Time', FMT) - datetime.strptime ('Start Time', FMT)
and got the list of strings of the format datetime.timedelta(seconds=1535)
I am stuck on how to get the seconds only out of the list of strings, as the object is not subscriptable, and also if there is a way to calculate the time difference in minutes/hours?
Solution 1:[1]
(datetime.strptime ('End Time', FMT) - datetime.strptime ('Start Time', FMT)).total_seconds()
should do the trick, to get seconds, at least since python 3.2. Please refer to documentation (https://docs.python.org/3/library/datetime.html#datetime.timedelta).
To get other time units, you could easily calculate them, or use time module, i.e.:
from time import strftime
from time import gmtime
strftime("%H:%M:%S", gmtime(td.total_seconds()))
where td is your timedelta object.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
