時間模組 time模組

2022-06-13 20:06:12 字數 1797 閱讀 4491

1.時間戳      time>time()   從1970 至今過了多少秒       (時間戳、格式化時間、時間物件)

print(time.time())

2. 格式時間   time.strftime(%y-%m-%d  %h-%m-%s)    str formate time    格式為字串

'''

%y year with century as a decimal number.

%m month as a decimal number [01,12].

%d day of the month as a decimal number [01,31].

%h hour (24-hour clock) as a decimal number [00,23].

%m minute as a decimal number [00,59].

%s second as a decimal number [00,61].

'''print(time.strftime('

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

#%x == %h:%m:%s

print(time.strftime('

%y-%m-%d %x'))

print(type(time.strftime('

%y-%m-%d %x

')))

#獲取年月

print(time.strftime('

%y/%m

'))

3.獲取時間物件   time.strptime(時間格式)、time.localtime() 當前時間物件

返回的是乙個元組, 元組中有9個值:

9個值分別代表: 年、月、日、時、分、秒、一周中第幾天,一年中的第幾天,夏令時(了解)

#3.獲取時間物件 (****)

print

(time.localtime())

print

(type(time.localtime()))

time_obj =time.localtime()

print

(time_obj.tm_year)

print

(time_obj.tm_year)

print

(time_obj.tm_mon)

time.struct_time(tm_year=2019, tm_mon=11, tm_mday=17, tm_hour=21, tm_min=48, tm_sec=28, tm_wday=6, tm_yday=321, tm_isdst=0)

'time.struct_time

'>

2019

2019

11

# res = time.strptime('2019-01-01', '%y-%m-%d')  time.strptime() 把格式化時間轉化為物件

# print(res)

# print(res.tm_min)

3.把時間物件轉化為格式化時間

#

res = time.localtime()

#time.sleep(5)

#獲取當前時間的格式化時間

#print(time.strftime('%y-%m-%d %h:%m:%s', time.localtime()))##

# 將時間物件轉為格式化時間

#print(time.strftime('%y-%m-%d %h:%m:%s', res))

time時間模組

一,時間戳 計算時間 import time print time.time 二,結構化時間,計算當下的時間 import time print time.localtime 結構化時間utc 將結構化時間轉化為時間戳 print time.mktime time.localtime 將結構化時間轉...

模組 time(時間)

import time import random print time.time 從1970 01 01到現在過了多少秒。time.time應用講解 先獲取啟動時間在獲取結束時間,結束時間減去啟動時間 執行時間 start time.time 記錄啟動時間 time.sleep random.ra...

time 時間模組

time 時間模組 import time time 獲取本地時間戳 res time.time print res mktime 通過 時間元組 獲取 時間戳 引數是時間元組 ttp 2019,5,16,10,55,30,0,0,0 res time.mktime ttp print res lo...