Course Progress45%
⏳ 7 min read
The datetime Module
Create, format, parse, and calculate with dates and times — the right tools for every calendar and scheduling problem.
"Dates are trickier than they look. The datetime module handles leap years, weekdays, and time arithmetic so you never have to count days manually again."
— ShurAIThree Key Classes
date
Year, month, day.
No time information.
No time information.
datetime
Date AND time together.
Most commonly used.
Most commonly used.
timedelta
A duration of time.
Used for date arithmetic.
Used for date arithmetic.
Working with date
python
from datetime import date
# Today’s date
today = date.today()
print(today) # 2026-03-15
print(today.year) # 2026
print(today.month) # 3
print(today.day) # 15
print(today.weekday()) # 0=Mon, 1=Tue ... 6=Sun
# Create any specific date
independence = date(1947, 8, 15)
print(independence) # 1947-08-15
Working with datetime
python
from datetime import datetime
# Current date AND time
now = datetime.now()
print(now) # 2026-03-15 14:35:22.456789
print(now.hour) # 14
print(now.minute) # 35
print(now.second) # 22
# Create a specific datetime (year, month, day, hour, minute, second)
launch = datetime(2026, 8, 15, 9, 30, 0)
print(launch) # 2026-08-15 09:30:00
Formatting Dates — strftime
strftime = “string from time” — converts a datetime to a human-friendly string:
python
from datetime import datetime
now = datetime.now()
print(now.strftime("%d %B %Y")) # 15 March 2026
print(now.strftime("%d/%m/%Y")) # 15/03/2026
print(now.strftime("%I:%M %p")) # 02:35 PM
print(now.strftime("%A, %d %B %Y")) # Saturday, 15 March 2026
# Parse a string INTO a datetime (strptime = "string parse time")
dt = datetime.strptime("15/08/2026", "%d/%m/%Y")
print(dt) # 2026-08-15 00:00:00
timedelta — Date Arithmetic
python
from datetime import date, timedelta
today = date.today()
week = timedelta(days=7)
print("Today :", today)
print("In 7 days :", today + week)
print("7 days ago :", today - week)
# Count days between two dates
start = date(2026, 1, 1)
end = date(2026, 12, 31)
print("Days in 2026:", (end - start).days) # 364
Real Example — Age Calculator
python
from datetime import date
def age_info(birth_year, birth_month, birth_day):
"""Print age and days until next birthday."""
today = date.today()
birthday = date(birth_year, birth_month, birth_day)
age = today.year - birthday.year
if (today.month, today.day) < (birthday.month, birthday.day):
age -= 1 # birthday hasn’t happened yet this year
next_bday = date(today.year, birthday.month, birthday.day)
if next_bday <= today:
next_bday = date(today.year + 1, birthday.month, birthday.day)
days_left = (next_bday - today).days
print(f"Age : {age} years old")
print(f"Next birthday: {next_bday.strftime('%d %B %Y')} ({days_left} days away)")
age_info(2000, 8, 15)
"strftime() formats a datetime as a string. strptime() parses a string into a datetime. Learn both and you can read or write any date format the world uses."
🧠 Quiz — Q1
What does date.today() return?
🧠 Quiz — Q2
What is a timedelta?
🧠 Quiz — Q3
What does now.strftime('%d/%m/%Y') do?
🧠 Quiz — Q4
What is the key difference between strftime and strptime?