date、time和datetime
一、簡介
我們在編寫代碼時,往往涉及到時間和日期的運用。時間日期又是一種特有的格式(<class 'datetime.datetime'>),這種格式不像我們常見數據格式容易操作,在使用的時候有諸多不便。例如我們想改變它的顯示樣式,或者按照一定的年、月等特性進行分類。但是我們可以對datetime進行格式轉換后操作。
進行格式轉化后,python中datetime模塊變的非常好用。在python中提供了日期格式和字符串格式相互轉化的函數strftime()和strptime()
- strftime():由日期格式轉化為字符串格式的函數
- strptime():由字符串格式轉化為日期格式的函數
二、datetime模塊中的數據類型
在python的標准庫中包含日期(date)和時間(time)數據的數據類型,datetime、time以及calendar模塊會被經常用到。datetime以毫秒形式存儲日期和時間,datetime.timedelta表示兩個datetime對象之間的時間差。給datetime對象加上或減去一個或多個timedelta,會產生一個新的對象。
三、引入時間日期模塊
1、引入datetime
from datetime import datetime
from datetime import timedelta
#生成一個現在時間日期類型
now = datetime.now()
delta = now - datetime(2017,6,27,10,10,10,10)
print(now,type(now))
#結果:2019-07-07 09:39:59.805549 <class 'datetime.datetime'>
print(now.date(),type(now.date()))
#結果:2019-07-07 <class 'datetime.date'>
print(now.time(),type(now.time()))
#結果:09:39:59.805549 <class 'datetime.time'>
print(delta,type(delta))
#結果:739 days, 23:29:49.805539 <class 'datetime.timedelta'>
print(delta.days,type(delta.days))
#結果:739 <class 'int'>
#datetime參數:datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])
2、引入time
import time # (1)當前時間戳 #time.time() one = time.time() print(one) #結果:1562468537.7579844 # (2)時間戳 → 時間元組,默認為當前時間 # time.localtime() 括號中放時間戳 two = time.localtime(time.time()) print(two) #結果:time.struct_time(tm_year=2019, tm_mon=7, tm_mday=7, tm_hour=11, tm_min=2, tm_sec=17, tm_wday=6, tm_yday=188, tm_isdst=0) print(time.localtime(123456)) #結果:time.struct_time(tm_year=1970, tm_mon=1, tm_mday=2, tm_hour=18, tm_min=17, tm_sec=36, tm_wday=4, tm_yday=2, tm_isdst=0 # (3)時間戳 → 可視化時間 # time.ctime(時間戳),默認為當前時間 three = time.ctime(1562467704.0623) print(three) #結果:Sun Jul 7 10:48:24 2019 # (4)時間元組 → 時間戳 # 1562463871.0 four = time.mktime((2019, 7, 7, 9, 44, 31, 6, 273, 0)) print(four) #結果:1562463871.0 # (5)時間元組 → 可視化時間 # time.asctime(時間元組),默認為當前時間 five = time.asctime((2019, 7, 7, 10, 48, 24, 6, 273, 0)) five1 = time.asctime(time.localtime(1562467704.0623)) print(five,five1) #結果:Sun Jul 7 10:48:24 2019 Sun Jul 7 10:48:24 2019 # (6)時間元組 → 可視化時間(定制) # time.strftime(要轉換成的格式,時間元組) six = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) print(six) #結果:2019-07-07 11:02:17 # (7)可視化時間(定制) → 時間元祖 # time.strptime(時間字符串,時間格式) print("時間元祖:",time.strptime('2019-7-7 11:32:23', '%Y-%m-%d %H:%M:%S')) #結果:時間元祖: time.struct_time(tm_year=2019, tm_mon=7, tm_mday=7, tm_hour=11, tm_min=32, tm_sec=23, tm_wday=6, tm_yday=188, tm_isdst=-1) # (8)將格式字符串轉換為時間戳 eight = "Sun Jul 7 10:48:24 2019" print (time.mktime(time.strptime(eight,"%a %b %d %H:%M:%S %Y"))) #結果:1562467704.0
3、時間元組
引入time模塊后就不得不提一下時間元組。時間元組就是Python函數用一個元組裝起來的9組數字,這9組數字不是憑空而來,而是根據時間年、月、日……組成,每組數據都有它特定的含義。
時間元組特性表
4、日期、時間格式化符號:
%y 兩位數的年份表示(00-99) %Y 四位數的年份表示(000-9999) %m 月份(01-12) %d 月內中的一天(0-31) %H 24小時制小時數(0-23) %I 12小時制小時數(01-12) %M 分鍾數(00=59) %S 秒(00-59) %a 本地簡化星期名稱 %A 本地完整星期名稱 %b 本地簡化的月份名稱 %B 本地完整的月份名稱 %c 本地相應的日期表示和時間表示 %j 年內的一天(001-366) %p 本地A.M.或P.M.的等價符 %U 一年中的星期數(00-53)星期天為星期的開始 %w 星期(0-6),星期天為星期的開始 %W 一年中的星期數(00-53)星期一為星期的開始 %x 本地相應的日期表示 %X 本地相應的時間表示 %Z 當前時區的名稱 %% %號本身
四、strptime 和 strftime 函數
時間.strftime(時間格式) datetime.strptime(字符串,時間格式) 示范: datetime.strptime(str,'%Y-%m-%d') datetime.now().strftime("%Y-%m-%d %H:%M:%S")
1、strptime
from datetime import datetime S=datetime.strptime('2019/07/07','%Y/%m/%d') print(S,type(S)) S=datetime.strptime('2019年7月7日星期日','%Y年%m月%d日星期日') print(S,type(S)) S=datetime.strptime('2019年7月7日星期日8時42分24秒','%Y年%m月%d日星期日%H時%M分%S秒') print(S,type(S)) S=datetime.strptime('7/7/2019','%m/%d/%Y') print(S,type(S)) S=datetime.strptime('7/7/2019 8:42:50','%m/%d/%Y %H:%M:%S') print(S,type(S)) #結果: 2019-07-07 00:00:00 <class 'datetime.datetime'> 2019-07-07 00:00:00 <class 'datetime.datetime'> 2019-07-07 08:42:24 <class 'datetime.datetime'> 2019-07-07 00:00:00 <class 'datetime.datetime'> 2019-07-07 08:42:50 <class 'datetime.datetime'>
2、strftime
dt=datetime.now() s=dt.strftime('%m/%d/%Y %H:%M:%S %p') print(s,type(s)) s=dt.strftime('%A,%B %d,%Y') print(s,type(s)) txt =('%s年%s月%s日星期%s %s時%s分%s秒'%(dt.strftime('%Y'),dt.strftime('%m'),dt.strftime('%d'),\
dt.strftime('%w'),dt.strftime('%H'),dt.strftime('%M'),dt.strftime('%S'))) print(txt) s=dt.strftime('%B %d,%Y') print(s,type(s)) 結果: 07/07/2019 14:57:17 PM <class 'str'> Sunday,July 07,2019 <class 'str'> 2019年07月07日星期0 14時57分17秒 July 07,2019 <class 'str'>
strftime中,結果“2019年07月07日星期0 14時57分17秒”,這其中的星期0,怎樣讓他正常顯示,有方法的朋友提示一下,在這里謝謝啦。