品易云推流 关闭
文章详情页
文章 > Python常见问题 > python如何把秒换成时分秒

python如何把秒换成时分秒

头像

yang

2020-05-19 11:43:1115450浏览 · 0收藏 · 0评论

在python中可以使用下面的方法将秒数转换为时分秒:

seconds=5555
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
print ("%02d:%02d:%02d" % (h, m, s))

输出结果:

01:32:35

python divmod() 函数把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b)。

2、使用strftime()与gmtime()函数把秒转换为时分秒

from time import strftime
from time import gmtime

strftime("%H:%M:%S", gmtime(5555))

输出结果如下:

'01:32:35'

gmtime() 函数将一个时间戳转换为UTC时区(0时区)的struct_time,可选的参数sec表示从1970-1-1以来的秒数。其默认值为time.time(),函数返回time.struct_time类型的对象。

strftime() 函数接收以时间元组,并返回以可读字符串表示的当地时间,格式由参数format决定。

更多Python知识请关注Python自学网

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图