python常用的時間處理模組time

2021-07-09 20:05:05 字數 1474 閱讀 1157

%y

: 年 2016

%m : 月

%d : 日

%w : 星期

%h : 時

%m : 分

%s : 秒

%b : 月份縮寫

%a : 星期縮寫

time.struct_time

time.struct_time(tm_year=2016, tm_mon=2, tm_mday=3, tm_hour=7, tm_min=8, tm_sec=16, tm_wday=2, tm_yday=34, tm_isdst=0)
time.time()

返回當前的秒,浮點形式,utc時間

1454483296.56
time.gmtime([secs])

將以秒形式表示的時間轉換成struct_time結構,utc時間

time.localtime([secs])

將以秒形式表示的時間轉換成struct_time結構,本地時間

time.ctime([secs])

將以秒形式表示的時間轉換成字串形式的本地時間

color="blue">print(time.ctime())font>

wed feb 03 15:24:03 2016

time.asctime([t])

將struct_time或tuple表示的時間轉變成字串形式

print(time.asctime(time.localtime()))

#wed feb 03 15:29:17 2016

time.mktime()

將struct_time或tuple表示的時間轉變成以秒形式表示的本地時間

print(time.mktime(time.localtime()))

#1454484622.0

time.strftime(format[,t])

將tuple或struct_time表示的時間轉變成指定的格式

print(time.strftime("%y-%m-%d

%h:%m:%s",time.localtime()))

#2016-02-03 15:36:56

time.strptime(string[,format])

將字串表示的時間,按照格式轉變成struct_time結構

print(time.strptime("03 feb 2016","%d

%b%y"))

time.struct_time(tm_year=2016, tm_mon=2, tm_mday=3, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=34, tm_isdst=-1)

Python時間處理 dateutil模組

dateutil模組主要有兩個函式,parser和rrule。easy install python dateutil pip install python dateutilparser是根據字串解析成datetime,字串可以很隨意,可以用時間日期的英文單詞,可以用橫線 逗號 空格等做分隔符。fr...

python之時間處理模組

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

python時間處理常用

import time 當前時間戳 time.time 1575430925.420576當前時間結構 time.localtime 時間結構 time.struct time tm year 2019 tm mon 12,tm mday 4,tm hour 11,tm min 43,tm sec ...