The datetime module supplies classes for manipulating dates and times in Python. It supports date and time arithmetic, formatting, parsing, and time zone management.
Installation:
Built-in module (No installation required)
Key Features:
- Work with dates (date), times (time), and combined date-time (datetime)
- Measure time spans and intervals using timedelta
- Format dates to strings (strftime) and parse strings to dates (strptime)
- Time zone aware calculations with timezone
Code Example:
from datetime import datetime, timedelta
# Get current date and time
now = datetime.now()
print("Current DateTime:", now.strftime("%Y-%m-%d %H:%M:%S"))
# Add 7 days to current date
future_date = now + timedelta(days=7)
print("Date in 7 days:", future_date.strftime("%Y-%m-%d"))
No comments:
Post a Comment