datetime Module
datetime Module
Section titled “datetime Module”Simple Analogy 🗓️
Section titled “Simple Analogy 🗓️”The datetime module is like a smart calendar + clock combined. Instead of manually counting days or parsing date strings, it handles all the complexity — leap years, time zones, date math.
Core Types
Section titled “Core Types”from datetime import date, time, datetime, timedelta
# date — just the dated = date(2025, 12, 25)print(d) # 2025-12-25print(d.year) # 2025print(d.month) # 12print(d.day) # 25print(d.weekday()) # 3 (Wednesday — Monday=0)
# time — just the timet = time(14, 30, 45)print(t) # 14:30:45print(t.hour) # 14print(t.minute) # 30
# datetime — date + timedt = datetime(2025, 12, 25, 14, 30, 45)print(dt) # 2025-12-25 14:30:45Getting Current Date/Time
Section titled “Getting Current Date/Time”from datetime import datetime, date, timezone
# Current datetimenow = datetime.now()print(now) # 2025-06-23 10:30:15.123456
# Current date onlytoday = date.today()print(today) # 2025-06-23
# Current UTC timeutc_now = datetime.now(timezone.utc)print(utc_now) # 2025-06-23 05:00:15.123456+00:00Formatting and Parsing
Section titled “Formatting and Parsing”from datetime import datetime
# Format a datetime to a stringdt = datetime(2025, 12, 25, 14, 30)
formatted = dt.strftime("%Y-%m-%d %H:%M:%S")print(formatted) # 2025-12-25 14:30:00
formatted = dt.strftime("%A, %B %d, %Y")print(formatted) # Thursday, December 25, 2025
formatted = dt.strftime("%I:%M %p")print(formatted) # 02:30 PM
# Parse a string to datetimedate_str = "2025-12-25 14:30:00"parsed = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")print(parsed) # 2025-12-25 14:30:00Common Format Codes
Section titled “Common Format Codes”| Code | Meaning | Example |
|---|---|---|
%Y | Year (4 digits) | 2025 |
%m | Month (01-12) | 12 |
%d | Day (01-31) | 25 |
%H | Hour (00-23) | 14 |
%M | Minute (00-59) | 30 |
%S | Second (00-59) | 45 |
%A | Weekday name | Thursday |
%B | Month name | December |
Timedelta — Date Math
Section titled “Timedelta — Date Math”from datetime import datetime, timedelta
now = datetime.now()
# Add/subtract timetomorrow = now + timedelta(days=1)next_week = now + timedelta(weeks=1)last_hour = now - timedelta(hours=1)in_90_days = now + timedelta(days=90)
# Difference between two datesstart = datetime(2025, 1, 1)end = datetime(2025, 12, 31)diff = end - startprint(diff.days) # 364print(diff.total_seconds()) # 31449600.0
# Find next Fridayfrom datetime import timedeltatoday = datetime.now()days_ahead = 4 - today.weekday() # Friday = 4if days_ahead <= 0: days_ahead += 7next_friday = today + timedelta(days=days_ahead)Time Zones
Section titled “Time Zones”from datetime import datetime, timezone, timedelta# For full timezone support, install: pip install pytz# Or use zoneinfo (Python 3.9+)from zoneinfo import ZoneInfo
utc = datetime.now(timezone.utc)print(utc) # 2025-06-23 05:00:00+00:00
# Convert to another timezone (Python 3.9+)ist = utc.astimezone(ZoneInfo("Asia/Kolkata"))print(ist) # 2025-06-23 10:30:00+05:30
est = utc.astimezone(ZoneInfo("America/New_York"))print(est) # 2025-06-23 01:00:00-04:00🧠 In Simple Words
Section titled “🧠 In Simple Words”date= day/month/year;time= hour/minute/second;datetime= both combined- Use
strftime()to format a date to a string; usestrptime()to parse a string to a date timedelta= date math — add/subtract days, hours, weeks, etc.- Compare dates with
<,>,==; subtract them to get atimedelta - For time zones, use
zoneinfo.ZoneInfo(Python 3.9+) or installpytz