新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:Python中time模塊的方法整理
1、時(shí)間戳time.time當(dāng)前時(shí)間。

2、time.sleep程序暫停三秒鐘。
3、time.ctime當(dāng)前時(shí)間。
年月日時(shí)分秒。
4、time.localtime()將時(shí)間戳轉(zhuǎn)換成元組。
顯示當(dāng)前時(shí)間的詳細(xì)信息。
time.mktime將時(shí)間元組轉(zhuǎn)換為時(shí)間戳。
time.strftime()#將元組時(shí)間轉(zhuǎn)換為字符串形式。
time.strptime()#將字符串轉(zhuǎn)換成元組。
實(shí)例
import time
t1 = time.time()
print(t1) #程序至此的執(zhí)行時(shí)間
# time.sleep(3) #程序至此暫停3秒
t2 = time.time()
print(t2)
s = time.ctime(t1) #當(dāng)前時(shí)間
print(s)
#將時(shí)間戳轉(zhuǎn)換為元組的形式(當(dāng)前時(shí)間詳細(xì)信息顯示)
loc = time.localtime(t1)
print(loc)
print(loc.tm_hour) #可調(diào)用元組里的具體內(nèi)容
print(loc.tm_mon)
#將(時(shí)間)元組轉(zhuǎn)為時(shí)間戳的形式
loc = time.mktime(loc)
print(loc) #小數(shù)點(diǎn)后清零
#將元組時(shí)間 轉(zhuǎn)為字符串形式
s = time.strftime('%Y-%m-%d')
print(s) #以年月日的形式打印
s = time.strftime('%Y-%m-%d %H:%M:%S')
print(s)
#將字符串轉(zhuǎn)成元組的方式
t = time.strptime('2021/9/13','%Y/%m/%d') #第一個(gè)參數(shù)為時(shí)間字符串,第二個(gè)參數(shù)為待轉(zhuǎn)換的格式
print(t)以上就是python中time模塊的方法整理,希望對(duì)大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)Python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
名稱欄目:創(chuàng)新互聯(lián)Python教程:Python中time模塊的方法整理
新聞來(lái)源:http://www.dlmjj.cn/article/ccocspp.html


咨詢
建站咨詢
