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

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ù)運(yùn)算結(jié)果結(jié)合起來,返回一個(gè)包含商和余數(shù)的元組(a // b, a % b)。
2、使用strftime()與gmtime()函數(shù)把秒轉(zhuǎn)換為時(shí)分秒
from time import strftime
from time import gmtime
strftime("%H:%M:%S", gmtime(5555))輸出結(jié)果如下:
'01:32:35'
gmtime() 函數(shù)將一個(gè)時(shí)間戳轉(zhuǎn)換為UTC時(shí)區(qū)(0時(shí)區(qū))的struct_time,可選的參數(shù)sec表示從1970-1-1以來的秒數(shù)。其默認(rèn)值為time.time(),函數(shù)返回time.struct_time類型的對(duì)象。
strftime() 函數(shù)接收以時(shí)間元組,并返回以可讀字符串表示的當(dāng)?shù)貢r(shí)間,格式由參數(shù)format決定。
分享名稱:創(chuàng)新互聯(lián)Python教程:python如何把秒換成時(shí)分秒
轉(zhuǎn)載來于:http://m.5511xx.com/article/cdjppjc.html


咨詢
建站咨詢
