python 多種日期時間處理函數(shù)實例詳解
Python 提供了多種日期和時間處理函數(shù),這些函數(shù)可以用于解析、格式化、計算和操作日期和時間。以下是一些常用的 Python 日期和時間處理函數(shù)及其用法:

日期和時間格式化函數(shù)
1. **strftime()**:用于格式化日期和時間對象。
2. **strptime()**:用于將字符串解析為日期和時間對象。
from datetime import datetime
# 當(dāng)前時間
current_time = datetime.now()
# 格式化日期和時間
formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S") # 輸出:2023-04-01 12:34:56
# 將字符串解析為日期和時間
parsed_time = datetime.strptime("2023-04-01 12:34:56", "%Y-%m-%d %H:%M:%S") # 輸出:2023-04-01 12:34:56日期和時間組件函數(shù)
1. **year()**:返回日期和時間中的年份。
2. **month()**:返回日期和時間中的月份。
3. **day()**:返回日期和時間中的天。
4. **hour()**:返回日期和時間中的小時。
5. **minute()**:返回日期和時間中的分鐘。
6. **second()**:返回日期和時間中的秒。
# 當(dāng)前時間 current_time = datetime.now() # 獲取日期和時間組件 year = current_time.year # 輸出:2023 month = current_time.month # 輸出:4 day = current_time.day # 輸出:1 hour = current_time.hour # 輸出:12 minute = current_time.minute # 輸出:34 second = current_time.second # 輸出:56
日期和時間計算函數(shù)
1. **timedelta()**:創(chuàng)建一個時間差對象。
2. **date()**:返回日期和時間對象的日期部分。
3. **time()**:返回日期和時間對象的時間部分。
4. **add()**:在日期和時間上添加時間差。
5. **subtract()**:從日期和時間中減去時間差。
# 當(dāng)前時間 current_time = datetime.now() # 創(chuàng)建時間差對象 time_delta = datetime.timedelta(days=1, hours=2, minutes=3, seconds=4) # 添加時間差 new_time = current_time + time_delta # 輸出:2023-04-02 14:07:00 # 減去時間差 old_time = current_time - time_delta # 輸出:2023-03-31 12:30:16
日期和時間生成函數(shù)
1. **date()**:創(chuàng)建一個日期對象。
2. **time()**:創(chuàng)建一個時間對象。
3. **datetime()**:創(chuàng)建一個日期和時間對象。
4. **now()**:返回當(dāng)前日期和時間。
5. **utcnow()**:返回 UTC 當(dāng)前日期和時間。
# 創(chuàng)建日期對象 date_obj = datetime.date(2023, 4, 1) # 輸出:2023-04-01 # 創(chuàng)建時間對象 time_obj = datetime.time(12, 34, 56) # 輸出:12:34:56 # 創(chuàng)建日期和時間對象 datetime_obj = datetime.datetime(2023, 4, 1, 12, 34, 56) # 輸出:2023-04-01 12:34:56 # 獲取當(dāng)前日期和時間 current_datetime = datetime.now() # 輸出:2023-04-01 12:34:56 # 獲取 UTC 當(dāng)前日期和時間 utc_current_datetime = datetime.utcnow() # # 輸出:2023-04-01 08:34:56
日期和時間差函數(shù)
1. **delta**:返回兩個日期和時間對象之間的差異。
# 當(dāng)前時間 current_time = datetime.now() # 另一個時間 another_time = datetime(2023, 4, 1, 10, 20, 30) # 計算時間差 time_delta = current_time - another_time
總結(jié)
Python 提供了豐富的日期和時間處理函數(shù),可以幫助你輕松地解析、格式化、計算和操作日期和時間。從格式化、組件提取、計算、轉(zhuǎn)換,到生成和差值比較,這些函數(shù)涵蓋了日期和時間處理的各個方面。掌握這些函數(shù)的使用,將使你在處理 Python 日期和時間時更加得心應(yīng)手。在實際應(yīng)用中,根據(jù)具體需求選擇合適的函數(shù),可以提高工作效率并簡化代碼。不斷練習(xí)和探索,你將能夠更加熟練地運用這些日期和時間處理函數(shù),為你的應(yīng)用程序增添更多的功能和靈活性。
到此這篇關(guān)于python 多種日期時間處理函數(shù)介紹的文章就介紹到這了,更多相關(guān)python 日期時間函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解用pyecharts Geo實現(xiàn)動態(tài)數(shù)據(jù)熱力圖城市找不到問題解決
這篇文章主要介紹了詳解用pyecharts Geo實現(xiàn)動態(tài)數(shù)據(jù)熱力圖城市找不到問題解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
Python數(shù)據(jù)結(jié)構(gòu)與算法之圖結(jié)構(gòu)(Graph)實例分析
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)與算法之圖結(jié)構(gòu)(Graph),結(jié)合實例形式分析了圖結(jié)構(gòu)的概念、原理、使用方法及相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
Python中號稱神仙的六個內(nèi)置函數(shù)詳解
這篇文章主要介紹了Python中號稱神仙的六個內(nèi)置函數(shù),今天分享的這6個內(nèi)置函數(shù),在使用?Python?進行數(shù)據(jù)分析或者其他復(fù)雜的自動化任務(wù)時非常方便,需要的朋友可以參考下2022-05-05
pycharm 主題theme設(shè)置調(diào)整仿sublime的方法
今天小編就為大家分享一篇pycharm 主題theme設(shè)置調(diào)整仿sublime的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05

