python 時間模組

2021-09-27 07:32:58 字數 1769 閱讀 8799

時間的一般表示

import time

s=『2019-7-14』

print(time.strptime(s,』%y-%m-%d』))

1.時間戳

print(time.time())
2.字串時間

print(time.ctime())
3.元組型別的時間

print(time.localtime())

info = time.localtime()

print(info)

print(info.tm_year) ##通過key找對應的value,顯示年份

print(info.tm_mon)

pwd_time = os.path.

getctime

(』/etc/passwd』)

print

(『pwd_time』,pwd_time) #時間戳

print

(time.

ctime

(pwd_time)

) #字串

print

(time.

localtime

(pwd_time)

)

tuple_time = time.

localtime()

print

(tuple_time) #元組時間

print

(time.

mktime

(tuple_time)

) #時間戳

print(time.

strftime

(』%m-

%d』,tuple_time))

print(time.

strftime

(』%f』,tuple_time))

print(time.

strftime

(』%t』,tuple_time)

)print(time.

strftime

(』%y-

%m-%d』,tuple_time))

##求三天前時間

threeday = matime - 606024*3

print(threeday)##列印結果為時間戳

threeday_localtime = time.localtime(threeday)

print(threeday_localtime)##列印結果為元組

threedays = time.strftime(』%y-%m-%d %h:%m:%s』,threeday_localtime)

print(threedays)##格式化時間

#計算2個小時前或後時間

now_time = datetime.now()

delta = timedelta(hours=2)

print(now_time+delta)

print(now_time-delta)

#時間差

now_time = datetime.now()##當前時間

pwd_time = os.path.gettime(』/etc/passwd』)

pwd_time_obj = datetime.fromtimestamp(pwd_time)

delta = now_time - pwd_time_obj

python 時間模組

import os import time s 2019 7 14 print time.strptime s,y m d s time 09 00 00 print time.strptime s time,h m s 把元組的時間轉換為時間戳 tuple time time.localtime ...

python 時間模組

格式化時間字串 y 兩位數的年份表示 00 99 y 四位數的年份表示 0000 9999 m 月份 01 12 d 月內的一天 0 31 h 24小時制的小時數 0 23 i 12小時制的小時數 01 12 m 分鐘數 00 59 s 秒 00 59 a 本地簡化星期名稱 a 本地完整星期名稱 b...

python 時間模組

時間表示的幾種形式 1.時間戳 2.字串時間 3.元組型別的時間 import os import time 1.時間戳 print time.time 2.字串時間 print time.ctime 3.元組時間 print time.localtime info time.localtime p...