python使用datetime模塊處理日期時(shí)間及常用功能詳解
導(dǎo)入 datetime 模塊
要使用 datetime
模塊,首先需要導(dǎo)入它:
import datetime
datetime 類
datetime
模塊提供了一個(gè)名為 datetime
的類,用于表示日期和時(shí)間。以下是一些創(chuàng)建 datetime
對(duì)象的方法:
1. 當(dāng)前日期和時(shí)間
要獲取當(dāng)前的日期和時(shí)間,可以使用 datetime.now()
方法:
import datetime current_datetime = datetime.datetime.now() print(f"Current date and time: {current_datetime}")
2. 指定日期和時(shí)間
要?jiǎng)?chuàng)建一個(gè)具有指定日期和時(shí)間的 datetime
對(duì)象,可以使用 datetime
類的構(gòu)造函數(shù):
import datetime dt = datetime.datetime(2023, 6, 6, 12, 30, 45) print(f"Specified date and time: {dt}")
在這個(gè)例子中,我們創(chuàng)建了一個(gè)表示 2023 年 6 月 6 日 12:30:45 的 datetime
對(duì)象。
date 類
datetime
模塊還提供了一個(gè)名為 date
的類,用于表示日期。以下是一些創(chuàng)建 date
對(duì)象的方法:
1. 當(dāng)前日期
要獲取當(dāng)前的日期,可以使用 date.today()
方法:
import datetime current_date = datetime.date.today() print(f"Current date: {current_date}")
2. 指定日期
要?jiǎng)?chuàng)建一個(gè)具有指定日期的 date
對(duì)象,可以使用 date
類的構(gòu)造函數(shù):
import datetime d = datetime.date(2023, 6, 6) print(f"Specified date: vvxyksv9kd")
在這個(gè)例子中,我們創(chuàng)建了一個(gè)表示 2023 年 6 月 6 日的 date
對(duì)象。
time 類
datetime
模塊還提供了一個(gè)名為 time
的類,用于表示時(shí)間。以下是一些創(chuàng)建 time
對(duì)象的方法:
1. 指定時(shí)間
要?jiǎng)?chuàng)建一個(gè)具有指定時(shí)間的 time
對(duì)象,可以使用 time
類的構(gòu)造函數(shù):
import datetime t = datetime.time(12, 30, 45) print(f"Specified time: {t}")
在這個(gè)例子中,我們創(chuàng)建了一個(gè)表示 12:30:45 的 time
對(duì)象。
timedelta 類
datetime
模塊提供了一個(gè)名為 timedelta
的類,用于表示時(shí)間間隔。以下是一些創(chuàng)建 timedelta
對(duì)象的方法:
1. 指定時(shí)間間隔
要?jiǎng)?chuàng)建一個(gè)具有指定時(shí)間間隔的 timedelta
對(duì)象,可以使用 timedelta
類的構(gòu)造函數(shù):
import datetime delta = datetime.timedelta(days=7, hours=2) print(f"Specified timedelta: {delta}")
在這個(gè)例子中,我們創(chuàng)建了一個(gè)表示 7 天 2 小時(shí)的時(shí)間間隔。
2. 運(yùn)算
可以使用 timedelta
對(duì)象進(jìn)行日期和時(shí)間的運(yùn)算。
示例:
import datetime current_date = datetime.date.today() delta = datetime.timedelta(days=7) future_date = current_date + delta print(f"Current date: {current_date}") print(f"Future date: {future_date}")
在這個(gè)例子中,我們計(jì)算了當(dāng)前日期后的 7 天的日期。
格式化日期和時(shí)間
strftime()
方法用于將 datetime
、date
和 time
對(duì)象格式化為字符串。以下是一些常用的格式化代碼:
%Y
:四位數(shù)的年份%m
:月份(01-12)%d
:日(01-31)%H
:小時(shí)(00-23)%M
:分鐘(00-59)%S
:秒(00-59)
示例:
import datetime current_datetime = datetime.datetime.now() formatted_datetime = current_datetime.strftime("%Y-%m-%d %H:%M:%S") print(f"Formatted date and time: {formatted_datetime}")
在這個(gè)例子中,我們將當(dāng)前日期和時(shí)間格式化為 "YYYY-MM-DD HH:MM:SS" 格式的字符串。
解析日期和時(shí)間
strptime()
方法用于將字符串解析為 datetime
對(duì)象。它需要兩個(gè)參數(shù):要解析的字符串和解析使用的格式。
示例:
import datetime date_string = "2023-06-06 12:30:45" format_string = "%Y-%m-%d %H:%M:%S" parsed_datetime = datetime.datetime.strptime(date_string, format_string) print(f"Parsed date and time: {parsed_datetime}")
在這個(gè)例子中,我們將 "2023-06-06 12:30:45" 格式的字符串解析為 datetime
對(duì)象。
替換日期和時(shí)間的部分值
replace()
方法用于替換 datetime
、date
和 time
對(duì)象的部分值。
示例:
import datetime current_datetime = datetime.datetime.now() new_datetime = current_datetime.replace(year=2024, month=1) print(f"Current date and time: {current_datetime}") print(f"New date and time: {new_datetime}")
在這個(gè)例子中,我們將當(dāng)前日期和時(shí)間的年份和月份替換為 2024 年 1 月。
以上就是 Python 標(biāo)準(zhǔn)庫(kù)中 datetime
模塊的基本介紹。通過(guò)這些實(shí)例,你應(yīng)該已經(jīng)對(duì)如何使用 datetime
模塊處理日期和時(shí)間有了一個(gè)基本的了解。在實(shí)際編程中,可以根據(jù)具體需求選擇合適的方法進(jìn)行日期和時(shí)間的操作,希望大家以后多多支持腳本之家!
- Python arrow模塊使用方法
- python中arrow庫(kù)用法大全
- 使用python?dateutil庫(kù)輕松處理日期和時(shí)間
- Python?dateutil庫(kù)簡(jiǎn)化日期時(shí)間處理利器使用場(chǎng)景實(shí)踐
- Python時(shí)間處理模塊Time和DateTime
- 封裝?Python?時(shí)間處理庫(kù)創(chuàng)建自己的TimeUtil類示例
- python簡(jiǎn)單幾步實(shí)現(xiàn)時(shí)間日期處理到數(shù)據(jù)文件的讀寫(xiě)
- Python Arrow處理時(shí)間數(shù)據(jù)使用詳解(標(biāo)準(zhǔn)庫(kù)之外另一種選擇)
相關(guān)文章
Python?Jupyter?Notebook導(dǎo)包報(bào)錯(cuò)問(wèn)題及解決
在conda環(huán)境中安裝包后,Jupyter?Notebook導(dǎo)入時(shí)出現(xiàn)ImportError,可能是由于包版本不對(duì)應(yīng)或版本太高,解決方法包括卸載并重新安裝出錯(cuò)的包,或安裝該包的其他版本,此外,確保在正確的環(huán)境中安裝ipykernel,并使用正確的命令添加和更改kernel2025-02-02Pyqt清空某一個(gè)QTreeewidgetItem下的所有分支方法
今天小編就為大家分享一篇Pyqt清空某一個(gè)QTreeewidgetItem下的所有分支方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06python 實(shí)現(xiàn)登錄網(wǎng)頁(yè)的操作方法
今天小編就為大家分享一篇python 實(shí)現(xiàn)登錄網(wǎng)頁(yè)的操作方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05