简体   繁体   English

从 datetime.timedelta 列表中获取时间

[英]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)并得到格式为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?我被困在如何仅从字符串列表中获取秒数,因为 object 不可下标,并且是否有办法计算以分钟/小时为单位的时间差?

(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.至少从 python 3.2 开始,应该做到这一点,以获得秒数。 Please refer to documentation ( https://docs.python.org/3/library/datetime.html#datetime.timedelta ).请参阅文档 ( https://docs.python.org/3/library/datetime.html#datetime.timedelta )。

To get other time units, you could easily calculate them, or use time module, ie:要获得其他时间单位,您可以轻松计算它们,或使用时间模块,即:

from time import strftime
from time import gmtime

strftime("%H:%M:%S", gmtime(td.total_seconds()))

where td is your timedelta object.其中td是您的时间增量 object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM