time和datetime時間戳---python
阿新 • • 發佈:2019-01-30
time模組
time模組提供各種操作時間的函式
說明:一般有兩種表示時間的方式:
1.時間戳的方式(相對於1970.1.1 00:00:00以秒計算的偏移量),時間戳是惟一的
2.以陣列的形式表示即(struct_time),共有九個元素,分別表示,同一個時間戳的struct_time會因為時區不同而不同
The tuple items are:
year (including century, e.g. 1998)
month (1-12)
day (1-31)
hours (0-23)
minutes (0-59)
seconds (0-59)
weekday (0-6, Monday is 0)
Julian day (day in the year, 1-366)
DST (Daylight Savings Time) flag (-1, 0 or 1)
<span style="font-family:FangSong_GB2312;font-size:18px;">
time() -- 返回時間戳
sleep() -- 延遲執行單位為s
gmtime() -- 轉換時間戳為時間元組(時間元組)
localtime() -- 轉換時間戳為本地時間物件
asctime() -- 將時間物件轉換為字串
ctime() -- 將使勁按戳轉換為字串
mktime() -- 將本地時間轉換為時間戳
strftime() -- 將時間物件轉換為規範性字串
</span>
常用的格式程式碼:
%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]. striptime() --將時間字串根據指定的格式化字元轉換成陣列形式的時間 print(time.time()) ---返回當前時間戳 print(time.ctime()) ---返回當前時間 print(time.ctime(time.time()-86400)) --將時間戳轉換為字串 print(time.localtime(time.time()-86400)) --本地時間 print(time.mktime(time.localtime())) --與time.localtime()功能相反,將struct_time格式轉回成時間戳格式 print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime())) -- 將struct_time格式轉換指定d字串格式 print(time.strptime("2016-01-28","%Y-%m-%d")) --將字串格式轉換成struct_time格式 time.sleep(5) 休眠5s
datetime 模組
定義的類有:
datetime 模組 定義的類有: datetime.date --表示日期的類。常用的屬性有year,month,day datetime.time ---表示時間的類。床用的屬性有hour,minute,second,microsecond datetime.datetime --表示日期時間 datetime.timedalta --表示時間間隔,即兩個時間點之間的長度 date類 date類表示日期,建構函式如下: datetime.date(year,month,day); year(1-9999) month(1-12) day(1-31) date.today()--返回一個表示當前本地日期的date物件 date.fromtimestamp(timestamp) -- 根據給定的時間戳,返回一個date物件 date.year() -- 取給定時間的年 date.month() -- 取時間物件的月 date.day() -- 取給定時間的日 date.replace() -- 生成一個新的日期物件,用引數指定的年,月, 日代替原有物件中的屬性 date.timetuple() -- 返回日期對應的time.struct_time物件 date.weekday() -- 返回week,Monday==0...Sunday == 6 date.isoweekday() -- 返回weekday,Monday == 1...Sunday == 7 date.ctime() -- 返回給定時間的字串格式 print(datetime.date.today().year) -- 取時間物件的年 print(datetime.date.today().month) --取時間物件的月 print(datetime.date.today().day) --取時間物件的日 print(datetime.date.today().replace(2010,6,12)) --生成一個新的日期物件,用引數指定的年,月,日代替原有物件中的屬性 print(datetime.date.today().timetuple()) 返回給定時間的時間元組/物件 print(datetime.date.today().weekday()) -- 返回weekday,從0開始 print(datetime.date.today().ctime) --返回給定時間的字串格格式 .tiem類 time類表示時間,由時,分,秒以及微秒組成 time.min() --最小表示時間 time.max() --最大表示時間 time.resolution() -- 微秒 案例: 最大時間 print(datetime.time.max) 最小時間 print(datetime.time.min) 時間最小單位,微秒 print(datetime.time.resolution) ·datetime類 datetime是date與time的結合體,包括date與time的所有資訊 datetime.max() --最大值 datetime.min() --最小值 datetime.resolution --datetime最小單位 datetime.today() -- 返回一個表示當前本地時間 datetime.fromtimestamp() --根據給定的時間戳,返回一個datetime物件 datetime.year() --取年 datetime.month() --取月 datetime.day() -- 取日期 datetim.replace() - 替換時間 datetime.strptime() --將字串轉換成日期格式 datetime.time() -- 取給定日期時間的時間 案例: print(datetime.datetime.max) #datetime最大值 print(datetime.datetime.min) #datetime最小值 print(datetime.datetime.resolution) #datetime最小單位 print(datetime.datetime.today()) #返回一個表示當前本地時間 print(datetime.datetime.fromtimestamp(time.time()))#根據給定的時間戮,返回一個datetime物件 print(datetime.datetime.now().year) #取時間物件的年 print(datetime.datetime.now().month) #取時間物件的月 print(datetime.datetime.now().day) #取時間物件的日 print(datetime.datetime.now().replace(2010,6,12))#生成一個新的日期物件,用引數指定的年,月,日代替原有物件中的屬性 print(datetime.datetime.now().timetuple()) #返回給定時間的時間元組/物件 print(datetime.datetime.now().weekday()) #返回weekday,從0開始 print(datetime.datetime.now().isoweekday()) #返回weekday,從1開始 print(datetime.datetime.now().ctime()) #返回給定時間的字串格式 print(datetime.datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M"))#將字串轉換成日期格式 print(datetime.datetime.now().time()) #取給定日期時間的時間 print(datetime.datetime.now() + datetime.timedelta(days=-5))#獲取5日前時間