Python處理日期和時(shí)間的方法總結(jié)
1 簡單入門
1.1 獲取當(dāng)前時(shí)間
import datetime datetime_object = datetime.datetime.now() print(datetime_object)
輸出
2022-03-29 16:36:44.749582
Process finished with exit code 0
1.2 獲取當(dāng)前日期
import datetime date_object = datetime.date.today() print(date_object)
輸出
2022-03-29
Process finished with exit code 0
1.3 datetime中的類
import datetime print(dir(datetime))
輸出
['MAXYEAR', 'MINYEAR', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'date', 'datetime', 'datetime_CAPI', 'sys', 'time', 'timedelta', 'timezone', 'tzinfo']
Process finished with exit code 0
2 datetime中的常用的類
- date類
- time類
- datetime類
- timedelta類
2.1 date類
(1)示例化date對(duì)象
import datetime d = datetime.date(2022, 3, 29) print(d)
輸出
2022-03-29
Process finished with exit code 0
(2)獲取當(dāng)前日期
from datetime import date today = date.today() print("當(dāng)前日期 =", today)
輸出
當(dāng)前日期 = 2022-03-29
Process finished with exit code 0
(3)從時(shí)間戳獲取日期
我們還可以從時(shí)間戳創(chuàng)建日期對(duì)象。Unix時(shí)間戳是特定日期到UTC的1970年1月1日之間的秒數(shù)。可以使用fromtimestamp()方法將時(shí)間戳轉(zhuǎn)換為日期。
from datetime import date timestamp = date.fromtimestamp(1576244364) print("日期 =", timestamp)
輸出
日期 = 2019-12-13
Process finished with exit code 0
(4)打印今天的年,月和日
from datetime import date # 今天的日期對(duì)象 today = date.today() print("當(dāng)前年:", today.year) print("當(dāng)前月:", today.month) print("當(dāng)前日:", today.day)
輸出
當(dāng)前年: 2022
當(dāng)前月: 3
當(dāng)前日: 29
Process finished with exit code 0
2.2 time類
(1)從time類示例化的時(shí)間對(duì)象表示本地時(shí)間。
from datetime import time # time(hour = 0, minute = 0, second = 0) a = time() print("a =", a) # time(hour, minute and second) b = time(11, 34, 56) print("b =", b) # time(hour, minute and second) c = time(hour = 11, minute = 34, second = 56) print("c =", c) # time(hour, minute, second, microsecond) d = time(11, 34, 56, 234566) print("d =", d)
輸出
a = 00:00:00
b = 11:34:56
c = 11:34:56
d = 11:34:56.234566
Process finished with exit code 0
(2)打印時(shí),分,秒和微秒
from datetime import time a = time(11, 34, 56) print("小時(shí)=", a.hour) print("分鐘=", a.minute) print("秒=", a.second) print("微秒=", a.microsecond)
輸出
小時(shí)= 11
分鐘= 34
秒= 56
微秒= 0
Process finished with exit code 0
2.3 datetime類
(1)datetime模塊有一個(gè)名為的dateclass類,可以包含來自date和time對(duì)象的信息。
from datetime import datetime #datetime(year, month, day) a = datetime(2019, 11, 28) print(a) # datetime(year, month, day, hour, minute, second, microsecond) b = datetime(2019, 11, 28, 23, 55, 59, 342380) print(b)
輸出
2019-11-28 00:00:00
2019-11-28 23:55:59.342380
Process finished with exit code 0
(2)打印年,月,時(shí),分和時(shí)間戳
from datetime import datetime a = datetime(2022, 3, 29, 23, 55, 59, 342380) print("年 =", a.year) print("月 =", a.month) print("日 =", a.day) print("時(shí) =", a.hour) print("份 =", a.minute) print("時(shí)間戳 =", a.timestamp())
輸出
年 = 2022
月 = 3
日 = 29
時(shí) = 23
份 = 55
時(shí)間戳 = 1648569359.34238
Process finished with exit code 0
2.4 datetime.timedelta類
(1)timedelta對(duì)象表示兩個(gè)日期或時(shí)間之間的時(shí)差。
from datetime import datetime, date t1 = date(year = 2018, month = 7, day = 12) t2 = date(year = 2017, month = 12, day = 23) t3 = t1 - t2 print("t3 =", t3) t4 = datetime(year = 2018, month = 7, day = 12, hour = 7, minute = 9, second = 33) t5 = datetime(year = 2019, month = 6, day = 10, hour = 5, minute = 55, second = 13) t6 = t4 - t5 print("t6 =", t6) print("type of t3 =", type(t3)) print("type of t6 =", type(t6))
輸出
t3 = 201 days, 0:00:00
t6 = -333 days, 1:14:20
type of t3 = <class 'datetime.timedelta'>
type of t6 = <class 'datetime.timedelta'>
Process finished with exit code 0
(2)兩個(gè)timedelta對(duì)象之間的時(shí)間差
from datetime import timedelta t1 = timedelta(weeks = 2, days = 5, hours = 1, seconds = 33) t2 = timedelta(days = 4, hours = 11, minutes = 4, seconds = 54) t3 = t1 - t2 print("t3 =", t3)
輸出
t3 = 14 days, 13:55:39
Process finished with exit code 0
到此這篇關(guān)于Python處理日期和時(shí)間的方法總結(jié)的文章就介紹到這了,更多相關(guān)Python日期時(shí)間處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于python if 判斷選擇結(jié)構(gòu)的實(shí)例詳解
代碼執(zhí)行結(jié)構(gòu)為順序結(jié)構(gòu)、選擇結(jié)構(gòu)、循環(huán)結(jié)構(gòu)。這篇文章主要介紹了python if 判斷選擇結(jié)構(gòu)的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05基于python實(shí)現(xiàn)百度語音識(shí)別和圖靈對(duì)話
這篇文章主要介紹了基于python實(shí)現(xiàn)百度語音識(shí)別和圖靈對(duì)話,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11基于python實(shí)現(xiàn)破解滑動(dòng)驗(yàn)證碼過程解析
這篇文章主要介紹了基于python實(shí)現(xiàn)破解滑動(dòng)驗(yàn)證碼過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05