Python datetime
最后修改:2025 年 2 月 15 日
在本文中,我们将展示如何在 Python 中使用 `datetime` 模块。`datetime` 模块提供了用于处理日期、时间和时间间隔的类。它在处理日期算术、格式化日期和解析日期字符串等任务时特别有用。
`datetime` 模块是 Python 标准库的一部分,因此无需额外安装。
datetime 的基本用法
以下示例演示了如何使用 `datetime` 模块来处理日期和时间。
main.py
from datetime import datetime # Get the current date and time now = datetime.now() print("Current Date and Time:", now) # Access individual components print("Year:", now.year) print("Month:", now.month) print("Day:", now.day) print("Hour:", now.hour) print("Minute:", now.minute) print("Second:", now.second)
在此程序中,使用 `datetime.now` 函数获取当前的日期和时间。可以通过 `datetime` 对象的属性访问各个组件(年、月、日、时、分、秒)。
$ python main.py Current Date and Time: 2025-02-15 12:34:56.789012 Year: 2025 Month: 2 Day: 15 Hour: 12 Minute: 34 Second: 56
格式化日期和时间
以下示例演示了如何使用 `strftime` 方法格式化日期和时间。
main.py
from datetime import datetime # Get the current date and time now = datetime.now() # Format the date and time formatted_date = now.strftime("%Y-%m-%d") formatted_time = now.strftime("%H:%M:%S") formatted_datetime = now.strftime("%Y-%m-%d %H:%M:%S") print("Formatted Date:", formatted_date) print("Formatted Time:", formatted_time) print("Formatted Date and Time:", formatted_datetime)
在此程序中,使用 `strftime` 方法将日期和时间格式化为不同的字符串表示形式。
$ python main.py Formatted Date: 2025-02-15 Formatted Time: 12:34:56 Formatted Date and Time: 2025-02-15 12:34:56
解析日期和时间
以下示例演示了如何使用 `strptime` 方法将日期字符串解析为 `datetime` 对象。
main.py
from datetime import datetime # Parse a date string date_string = "2025-02-15 12:34:56" parsed_date = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S") print("Parsed Date:", parsed_date)
在此程序中,使用 `strptime` 方法将日期字符串解析为 `datetime` 对象。
$ python main.py Parsed Date: 2025-02-15 12:34:56
日期算术
以下示例演示了如何使用 `timedelta` 类执行日期算术。
main.py
from datetime import datetime, timedelta # Get the current date and time now = datetime.now() # Add 5 days to the current date future_date = now + timedelta(days=5) # Subtract 2 weeks from the current date past_date = now - timedelta(weeks=2) print("Current Date:", now) print("Future Date (5 days later):", future_date) print("Past Date (2 weeks earlier):", past_date)
在此程序中,使用 `timedelta` 类将时间间隔加到 `datetime` 对象上或从 `datetime` 对象中减去。
$ python main.py Current Date: 2025-02-15 12:34:56.789012 Future Date (5 days later): 2025-02-20 12:34:56.789012 Past Date (2 weeks earlier): 2025-02-01 12:34:56.789012
处理时区
以下示例演示了如何使用 `pytz` 库处理时区。
main.py
from datetime import datetime import pytz # Get the current time in UTC utc_now = datetime.now(pytz.utc) print("Current Time in UTC:", utc_now) # Convert UTC time to a specific time zone eastern = pytz.timezone('US/Eastern') eastern_time = utc_now.astimezone(eastern) print("Current Time in US/Eastern:", eastern_time)
在此程序中,使用 `pytz` 库处理时区。首先以 UTC 检索当前时间,然后将其转换为 US/Eastern 时区。
$ python main.py Current Time in UTC: 2025-02-15 12:34:56.789012+00:00 Current Time in US/Eastern: 2025-02-15 07:34:56.789012-05:00
来源
在本文中,我们展示了如何在 Python 中使用 `datetime` 模块来处理日期、时间和时间间隔。`datetime` 模块是处理日期和时间操作的强大工具。
作者
列出所有 Python 教程。