Python:将减去的unix时间转换为小时/分钟/秒(Python: Convert subtracted unix time into hours/mins/seconds)

系统教程 行业动态 更新时间:2024-06-14 16:59:47
Python:将减去的unix时间转换为小时/分钟/秒(Python: Convert subtracted unix time into hours/mins/seconds)

我收到了三种不同的unixtimes

lastSeen = 1416248381 firstSeen = 1416248157

最后一个是lastSeen - firstSeen:

duration = 224

现在,我可以将lastSeen和firstSeen转换成日期时间没有问题。 但我在这段时间里遇到了麻烦。

我不确定如何将持续时间转换为分钟/秒等。 有没有人有任何想法,如果这可以做到?

I have received three different unixtimes

lastSeen = 1416248381 firstSeen = 1416248157

and the last one is lastSeen - firstSeen:

duration = 224

Now, I can convert lastSeen and firstSeen into a datetime no problem. But I am having trouble with the duration.

I am unsure how to convert the duration into something like minutes/seconds. Does anyone have any idea if this can be done?

最满意答案

您需要将秒转换为小时与分钟,并且您可以使用日期时间来完成

import datetime lastSeen = 1416248381 firstSeen = 1416248157 duration = lastSeen - firstSeen str(datetime.timedelta(seconds=duration))

输出将是: '0:03:44' : '0:03:44' : '0:03:44'

没有str()函数,你将得到: datetime.timedelta(0, 224)


使用时间

import time time.strftime("%H:%M:%S", time.gmtime(duration))

输出将是: '0:03:44' : '0:03:44' : '0:03:44'

You need to convert your seconds to Hours&Minutes and you can do it using datetime

import datetime lastSeen = 1416248381 firstSeen = 1416248157 duration = lastSeen - firstSeen str(datetime.timedelta(seconds=duration))

The ouput will be: '0:03:44'

Without the str() function you'll have: datetime.timedelta(0, 224)


Using time

import time time.strftime("%H:%M:%S", time.gmtime(duration))

The ouput will be: '0:03:44'

更多推荐

本文发布于:2023-04-17 09:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/4d0a2c86f992b20e868bf605fb56e9d3.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   小时   时间   Python   unix

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!