python如何把秒换成时分秒
发布时间:2026-03-18 22:12:09

以下方法可用于将秒数转换为时分秒:
seconds=5555
m,s=pmod(seconds,60)
h,m=pmod(m,60)
print("%02d:%02d:%02d"%(h,m,s))输出结果:
01:32:35
python pmod() 函数将除数和余数运算结果结合起来,返回包含商和余数的元组(a // b, a % b)。
2、使用strftime()和gmtime()函数将秒转换为时分秒
fromtimeimportstrftime
fromtimeimportgmtime
strftime("%H:%M:%S",gmtime(5555))输出结果如下:
'01:32:35'
gmtime() 函数将时间戳转换为UTC时区(0时区)的struct_time,可选参数sec表示自1970-1-1以来的秒数。其默认值为time.time()函数返回timee.struct_time类型的对象。
strftime() 函数以时间元组接收,并返回可读字符串表示的当地时间,格式由参数format决定。
请关注Python自学网了解更多Python知识。
下一篇 python如何从键盘输入数据?
