python之時間模組

2021-10-11 22:42:17 字數 1231 閱讀 6903

# 格式:

# 時間戳 2023年到現在經歷的秒數

# 用於時間間隔的計算

print

(time.time())

# 按照格式顯示

# 用於展示時間

print

(time.strftime(

"%y-%m-%d %h:%m:%s"))

print

(time.strftime(

"%y-%m-%d %h:%m:%s %p"))

print

(time.strftime(

"%y-%m-%d %x"))

# 結構化時間

# # 用於單獨獲取時間的某一部分

res = time.localtime(

)# 本地時間,時區時間 time.gmtime()世界標準時間,0時區

print

("res: "

, res)

# res: time.struct_time(tm_year=2020, tm_mon=9, tm_mday=30, tm_hour=11, tm_min=6, tm_sec=5, tm_wday=2, tm_yday=274, tm_isdst=0)

print

(res.tm_year)

# 2020

# 時間模組的轉換(從左到右,從上到下,pflm,記住時間轉換圖)

# 1980-3-22 10:10:10 轉換成秒

struc_time = time.strptime(

"1980-3-22 10:10:10"

,"%y-%m-%d %x"

)time_chuo = time.mktime(struc_time)

print

(time_chuo)

#datetime

import datetime

print

(datetime.datetime.now())

print

(datetime.datetime.now(

)+ datetime.timedelta(days=3)

)# 三天後

python模組之時間模組

表示時間的方式分為 1時間戳 timestamp 2格式化化的時間字串 format string 3結構化時間 struct time 時間戳,浮點型 print time.strftime y m d x 格式化時間 print time.localtime 本時區時間,struct time ...

python之時間處理模組

time模組 處理日期和時間函式 from datetime import datetime 獲取當前的時間 current datetime.now 獲取今天的時間 today datetime.today 建立乙個時間 create time datetime 2020,12 31,15 30 ...

Python之時間處理

一.關於時間幾種常用的處理方法 time包和datetime包 1 引入time包,import time 1.獲取從1970年到現在的秒數,至於為什麼從1970年開始這裡就不過多闡述了 我不會告訴你其實我也不知道 timeline time.time print timeline 輸出結果 153...