新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python如何把秒換成時分秒
在python中可以使用下面的方法將秒數(shù)轉(zhuǎn)換為時分秒:

seconds=5555
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
print ("%02d:%02d:%02d" % (h, m, s))輸出結(jié)果:
01:32:35
python divmod() 函數(shù)把除數(shù)和余數(shù)運算結(jié)果結(jié)合起來,返回一個包含商和余數(shù)的元組(a // b, a % b)。
2、使用strftime()與gmtime()函數(shù)把秒轉(zhuǎn)換為時分秒
from time import strftime
from time import gmtime
strftime("%H:%M:%S", gmtime(5555))輸出結(jié)果如下:
'01:32:35'
gmtime() 函數(shù)將一個時間戳轉(zhuǎn)換為UTC時區(qū)(0時區(qū))的struct_time,可選的參數(shù)sec表示從1970-1-1以來的秒數(shù)。其默認值為time.time(),函數(shù)返回time.struct_time類型的對象。
strftime() 函數(shù)接收以時間元組,并返回以可讀字符串表示的當?shù)貢r間,格式由參數(shù)format決定。
分享題目:創(chuàng)新互聯(lián)Python教程:python如何把秒換成時分秒
本文URL:http://fisionsoft.com.cn/article/cdjppjc.html


咨詢
建站咨詢
