【Python基础】python如何把秒换成时分秒
2021/12/12 17:19:54
本文主要是介绍【Python基础】python如何把秒换成时分秒,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1、在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))
gmtime() 函数将一个时间戳转换为UTC时区(0时区)的struct_time,可选的参数sec表示从1970-1-1以来的秒数。其默认值为time.time(),函数返回time.struct_time类型的对象。
strftime() 函数接收以时间元组,并返回以可读字符串表示的当地时间,格式由参数format决定。
参考文献
[1]python如何把秒换成时分秒
这篇关于【Python基础】python如何把秒换成时分秒的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python