python日期函数_python 时间相关函数
python 中與時(shí)間處理相關(guān)的模塊包括 time、datetime、以及 calendar
time 模塊
time() 函數(shù):time() 函數(shù)用于返回當(dāng)前時(shí)間的時(shí)間戳(1970年01月08時(shí)00分00秒到現(xiàn)在的浮點(diǎn)秒數(shù))
time() 函數(shù)的語法:time.time() //此語句中的第一個(gè) time 指的是 time 模塊,該函數(shù)參數(shù)列表為空
代碼:
1 importtime2
3 print('當(dāng)前時(shí)間的時(shí)間戳:%f' % time.time())
View Code
localtime([secs]) 函數(shù)
localtime() 函數(shù)的作用是格式化時(shí)間戳為本地 struct_time。如果 secs 參數(shù)未輸入則默認(rèn)為當(dāng)前時(shí)間
localtime() 函數(shù)語法:time.localtime([secs]) //time指的是 time 模塊,可選參數(shù) secs 表示 1970-1-1 到現(xiàn)在的秒數(shù)
代碼:
1 importtime2
3 print('time.localtime():', time.localtime())4 #輸出:
5 #time.localtime(): time.struct_time(tm_year=2018, tm_mon=4, tm_mday=17, tm_hour=15, tm_min=20, tm_sec=12, tm_wday=1, tm_yday=107, tm_isdst=0)
6 #最后三個(gè)輸出的值的含義為 一周的第幾日 0 ~ 6(0 是周一),一年中的第幾日,夏令時(shí)(-1, 0, 1, -1)
View Code
gmtime([secs]) 函數(shù)
gmtime() 函數(shù)用于將一個(gè)時(shí)間戳轉(zhuǎn)換成 UTC 時(shí)區(qū)(0 時(shí)區(qū))的 struct_time,可選參數(shù) secs 表示 1970-1-1 到現(xiàn)在的秒數(shù),若 secs 參數(shù)未輸入則默認(rèn)為當(dāng)前時(shí)間
gmtime() 函數(shù)的語法: time.gmtime([secs])
代碼:
1 importtime2
3 print('time.gmtime():', time.gmtime())4 #輸出:
5 #time.gmtime(): time.struct_time(tm_year=2018, tm_mon=4, tm_mday=17, tm_hour=7, tm_min=32, tm_sec=54, tm_wday=1, tm_yday=107, tm_isdst=0)
View Code
mktime(t) 函數(shù)
mktime() 函數(shù)用于執(zhí)行與 gmtime()、localtime() 相反的操作,接收 struct_time 對(duì)象作為參數(shù),返回用秒表示時(shí)間的浮點(diǎn)數(shù)。如果輸入不合法的時(shí)間則觸發(fā) OverflowError 或 ValueError 異常
代碼:
1 importtime2
3 t = (2018, 4, 17, 15, 20, 12, 1, 107, 0);4 print('time.mktime(t): %f' %time.mktime(t))5 #輸出:
6 #time.mktime(t): 1523949612.000000
View Code
asctime([t]) 函數(shù)
asctime() 函數(shù)用于接收時(shí)間元組(struct_time)并返回一個(gè)可讀形式為 Tue Apr 17 15:45:13 2018 的 24 個(gè)字符的字符串
部輸出參數(shù) t 則默認(rèn)為當(dāng)前時(shí)間點(diǎn)
代碼:
1 importtime2
3 t =time.localtime()4 print('time.asctime(t): %s' %time.asctime(t))5 #輸出:
6 #time.asctime(t): Tue Apr 17 15:45:13 2018
View Code
ctime([secs]) 函數(shù)
ctime 函數(shù)用于將一個(gè)時(shí)間戳轉(zhuǎn)化為 time.asctime() 的形式。若為指定參數(shù) secs 則默認(rèn)將 time.time() 作為參數(shù)
代碼:
1 importtime2
3 print('time.ctime(): %s' %time.ctime())4 #輸出:
5 #time.ctime(): Tue Apr 17 15:51:00 2018
View Code
sleep(secs) 函數(shù)
sleep() 函數(shù)用于推遲調(diào)用線程的運(yùn)行,可通過參數(shù) secs 指定進(jìn)程掛起的時(shí)間
sleep() 函數(shù)語法:time.sleep(escs) //其中 time 是指 time 模塊,secs 指推遲執(zhí)行的秒數(shù),此函數(shù)沒有返回值
1 importtime2
3 print('start: %s' %time.ctime())4 time.sleep(5)5 print('end: %s' %time.ctime())6 #輸出:
7 #start: Tue Apr 17 16:00:16 2018
8 #end: Tue Apr 17 16:00:21 2018
View Code
clock() 函數(shù)
clock() 函數(shù)用于以浮點(diǎn)數(shù)計(jì)算的秒數(shù)返回當(dāng)前 cpu 時(shí)間,用來衡量不同程度程序的耗時(shí)。該函數(shù)在不同的系統(tǒng)上含義不同。在 UNIX 系統(tǒng)上,返回的是 “進(jìn)程時(shí)間”,是用秒表示的浮點(diǎn)數(shù)(時(shí)間戳)。在 Windows 中,第一次調(diào)用返回的是進(jìn)程運(yùn)行的實(shí)際時(shí)間,第二次之后的調(diào)用返回的是自第一次調(diào)用后到現(xiàn)在的運(yùn)行時(shí)間
代碼:
1 importtime2
3 defprocedure():4 time.sleep(2)5
6 #measure process time
7 procedure()#sleep的時(shí)間不算程序運(yùn)行的時(shí)間,sleep()是沒有占用cpu
8 t1 =time.clock()9 print(t1)#程序運(yùn)行的時(shí)間
10 procedure()11 print('seconds process time:', time.clock())#第一次調(diào)用clock到本次調(diào)用clock的時(shí)間間隔
12
13 #measure wall time
14 t2 =time.time()15 procedure()16 print('seconds wall time:', time.time() -t2)17 #輸出:
18 #0.0
19 #seconds process time: 2.0004229494329526
20 #seconds wall time: 2.000399351119995
View Code
strftime(format[, t]) 函數(shù)
用于接收時(shí)間 struct_time 元組,并返回可讀字符串表示的當(dāng)?shù)貢r(shí)間,格式由參數(shù) format 決定
time 指的是 time 模塊,format 指格式化字符串,t 指可選的參數(shù),是一個(gè) struct_time 對(duì)象
代碼:
1 importtime2
3 t = (2016, 9, 25, 17, 50, 38, 6, 48, 0)4 t = time.mktime(t)#將 struct_time 轉(zhuǎn)換成浮點(diǎn)秒數(shù)
5 print(time.strftime('%b %d %Y %H : %M : %S', time.gmtime(t)))#gmtime()將時(shí)間戳轉(zhuǎn)換成0時(shí)區(qū)struct_time
6 #輸出:
7 #Sep 25 2016 09 : 50 : 38
View Code
strptime(string[, format]) 函數(shù)
strptime 函數(shù)用于根據(jù)指定的格式把一個(gè)時(shí)間字符串解析成時(shí)間元組 struct_time,和 strftime 的作用剛好相反
其中 time 指的是 time 模塊,string 指時(shí)間字符串,format 指格式化字符串
代碼:
1 importtime2
3 struct_time = time.strptime("25 Sep 16", "%d %b %y")4 print(struct_time)5 #輸出:
6 #time.struct_time(tm_year=2016, tm_mon=9, tm_mday=25, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=269, tm_isdst=-1)
View Code
datetime 模塊
today()
語法:datetime.datetime.today() //其中 datetime.datime 指的是 datetime.datime 類
代碼:
1 importdatetime2
3 print('today is:', datetime.datetime.today())4 #輸出:
5 #today is: 2018-04-19 19:38:59.625746
View Code
now([tz])
語法: datetime.datetime.now([tz]) // 后去 tz 參數(shù)所指時(shí)區(qū)的本地時(shí)間
代碼:
1 importdatetime2
3 print('now is:', datetime.datetime.now())4 #輸出:
5 #now is: 2018-04-19 19:42:43.731264
View Code
datetime.utcnow()
語法:datetime.datetime.utcnow() //返回一個(gè)當(dāng)前 utc 時(shí)間的 datetime 對(duì)象
代碼:
1 importdatetime2
3 print('utcnow is:', datetime.datetime.utcnow())4 #輸出:
5 #utcnow is: 2018-04-19 11:46:16.801027
View Code
fromtimestmap(timestamp[, tz])
根據(jù)時(shí)間戳創(chuàng)建一個(gè) datetime 對(duì)象。其中 tz 指定時(shí)區(qū)信息。返回一個(gè) datetime 對(duì)象
代碼:
1 importdatetime, time2
3 print('fromtimestamp is:', datetime.datetime.fromtimestamp(time.time()))4 #輸出:
5 #fromtimestamp is: 2018-04-19 19:50:01.279816
View Code
utcfromtimestamp(timestamp)
根據(jù)時(shí)間戳創(chuàng)建一個(gè) datetime 對(duì)象。其中,timestamp 指時(shí)間戳,返回一個(gè) datetime 對(duì)象
代碼:
1 importdatetime, time2
3 print('utcfromtimestamp is:', datetime.datetime.utcfromtimestamp(time.time()))4 #輸出:
5 #utcfromtimestamp is: 2018-04-19 11:54:30.934395
View Code
strptime(date_string, format)
將格式字符串轉(zhuǎn)換成 datetime 對(duì)象
其中 date_string 指日期字符串,format 為格式化方式,返回一個(gè) datetime 對(duì)象
代碼:
1 importdatetime2
3 dt =datetime.datetime.now()4 print('strptime is:', dt.strptime(str(dt), '%Y-%m-%d %H:%M:%S.%f'))5 #輸出:
6 #strptime is: 2018-04-19 20:07:49.020946
View Code
strftime(format)
將格式化字符串轉(zhuǎn)換為 datetime 對(duì)象
其中 format 為格式化方式
代碼:
1 importdatetime2
3 dt =datetime.datetime.now()4 print('strftime is:', dt.strftime('%Y-%m-%d %H:%M:%S'))5 #輸出:
6 #strftime is: 2018-04-19 20:11:17
View Code
calendar 模塊(日歷)
calendar.calendar(year, w = 2, l = 1, c = 6)
該函數(shù)返回一個(gè)多行字符串格式的 year 年歷,3 個(gè)月一行,間隔距離為 c。每日寬度間隔為 w 字符。每行長(zhǎng)度為 21 * w + 18 + 2 * c。l 是每星期行數(shù)
代碼:
1 importcalendar2
3 print(calendar.calendar(2018, w = 2, l = 1, c = 6))4 '''
5 輸出:6 20187
8 January February March9 Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su10 1 2 3 4 5 6 7 1 2 3 4 1 2 3 411 8 9 10 11 12 13 14 5 6 7 8 9 10 11 5 6 7 8 9 10 1112 15 16 17 18 19 20 21 12 13 14 15 16 17 18 12 13 14 15 16 17 1813 22 23 24 25 26 27 28 19 20 21 22 23 24 25 19 20 21 22 23 24 2514 29 30 31 26 27 28 26 27 28 29 30 3115
16 April May June17 Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su18 1 1 2 3 4 5 6 1 2 319 2 3 4 5 6 7 8 7 8 9 10 11 12 13 4 5 6 7 8 9 1020 9 10 11 12 13 14 15 14 15 16 17 18 19 20 11 12 13 14 15 16 1721 16 17 18 19 20 21 22 21 22 23 24 25 26 27 18 19 20 21 22 23 2422 23 24 25 26 27 28 29 28 29 30 31 25 26 27 28 29 3023 3024
25 July August September26 Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su27 1 1 2 3 4 5 1 228 2 3 4 5 6 7 8 6 7 8 9 10 11 12 3 4 5 6 7 8 929 9 10 11 12 13 14 15 13 14 15 16 17 18 19 10 11 12 13 14 15 1630 16 17 18 19 20 21 22 20 21 22 23 24 25 26 17 18 19 20 21 22 2331 23 24 25 26 27 28 29 27 28 29 30 31 24 25 26 27 28 29 3032 30 3133
34 October November December35 Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su36 1 2 3 4 5 6 7 1 2 3 4 1 237 8 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 938 15 16 17 18 19 20 21 12 13 14 15 16 17 18 10 11 12 13 14 15 1639 22 23 24 25 26 27 28 19 20 21 22 23 24 25 17 18 19 20 21 22 2340 29 30 31 26 27 28 29 30 24 25 26 27 28 29 3041 3142 '''
View Code
calendar.isleap(year)
如果是閏年就返回 True,否則返回 False
代碼:
1 importcalendar2
3 print(calendar.isleap(2018))4 #輸出:
5 #False
View Code
calendar.leapdays(y1, y2)
返回 y1, y2 之間的閏年總數(shù)
總結(jié)
以上是生活随笔為你收集整理的python日期函数_python 时间相关函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python爬虫贴吧_Python爬虫如
- 下一篇: python socket清空接受区_用