Lesson 12 of 16 · Working With Text and Data
Dates and Times
Work with dates and times in Python: the datetime module, date arithmetic with timedelta, formatting with strftime, parsing with strptime and time zones.
- 10 min read
- Intermediate
- Reviewed
- How to create dates and times with the datetime module
- How to add, subtract and compare dates
- How to turn dates into text and text into dates
- Why time zones matter
The datetime module
Python's built-in datetime module has a few types you will use all the time:
| Type | Holds | Example |
|---|---|---|
date | a calendar day | 17 November 2026 |
datetime | a day and a time | 17 November 2026, 09:30 |
timedelta | a length of time | 3 days, 4 hours |
Creating dates
from datetime import date, datetime
launch = date(2026, 11, 17)
meeting = datetime(2026, 11, 17, 9, 30)
print(launch)
print(meeting)
print(launch.year, launch.month, launch.day)
print(launch.strftime("%A"))The arguments go from largest to smallest: year, month, day, then hour and minute. .strftime("%A") gives the name of the weekday.
To get the current date and time, use date.today() and datetime.now().
from datetime import date, datetime print(date.today()) print(datetime.now())
Your output will differ from anyone else's, because it is the moment you press Run.
Doing maths with dates: timedelta
A timedelta is a length of time. Add it to a date to move forward, or subtract it to move back.
from datetime import date, timedelta start = date(2026, 11, 17) print(start + timedelta(days=30)) print(start - timedelta(weeks=2)) print(start + timedelta(days=45))
Python knows how many days each month has, and about leap years, so you never have to.
Subtracting one date from another gives a timedelta. Its .days tells you how many days lie between them.
from datetime import date today = date(2026, 11, 17) exam = date(2027, 1, 25) gap = exam - today print(gap) print(gap.days, "days to go")
Dates can also be compared with <, > and ==, and sorted like numbers.
from datetime import date deadlines = [date(2027, 3, 1), date(2026, 12, 15), date(2027, 1, 10)] print(min(deadlines)) print(sorted(deadlines)) print(date(2026, 12, 15) < date(2027, 1, 10))
From date to text: strftime
.strftime() (string from time) formats a date as text, using codes that start with %.
from datetime import datetime
moment = datetime(2026, 11, 17, 14, 5)
print(moment.strftime("%d/%m/%Y"))
print(moment.strftime("%d %B %Y"))
print(moment.strftime("%a %d %b, %H:%M"))
print(moment.strftime("%I:%M %p"))| Code | Meaning | Example |
|---|---|---|
%d %m %Y | day, month, four-digit year | 17 11 2026 |
%B %b | month name, short month name | November Nov |
%A %a | weekday, short weekday | Tuesday Tue |
%H %M | hour (24-hour clock), minute | 14 05 |
%I %p | hour (12-hour clock), AM or PM | 02 PM |
The same codes work inside an f-string: f"{moment:%d %b %Y}".
From text to date: strptime and ISO format
datetime.strptime() (string parse time) reads text into a date, using the same codes to describe the format.
from datetime import datetime
signup = datetime.strptime("25/12/2026 18:30", "%d/%m/%Y %H:%M")
print(signup)
print(signup.strftime("%A"))Many APIs and databases send dates in the international ISO 8601 format, 2026-11-17T09:30:00. Python reads it directly with fromisoformat(), and writes it with isoformat().
from datetime import datetime
created = datetime.fromisoformat("2026-11-17T09:30:00")
print(created.hour, created.minute)
print(created.isoformat())Time zones
A datetime without a time zone is called naive: Python does not know where in the world it applies. That is fine for a personal script, but a website with users in many countries needs aware times that carry their zone.
from datetime import datetime, timedelta, timezone utc_time = datetime(2026, 11, 17, 12, 0, tzinfo=timezone.utc) india = timezone(timedelta(hours=5, minutes=30)) print(utc_time) print(utc_time.astimezone(india))
Noon in UTC is 17:30 in India, which is 5 hours and 30 minutes ahead. A common rule in real projects: store times in UTC, and convert to the user's zone only when you show them. For named zones such as "Asia/Kolkata" or "Europe/London", which also handle summer time, use the zoneinfo module.
from zoneinfo import ZoneInfo
now_in_london = datetime.now(ZoneInfo("Europe/London"))Common mistakes
Work out when a six-week course ends, and how long it lasts.
from datetime import date, timedelta start = date(2026, 11, 17) # A course takes 6 weeks. # 1. Work out the end date: start + 6 weeks # 2. Print it as day, month name and year, e.g. 29 December 2026 # 3. Print how many days the course lasts # Output must be exactly: # Ends on 29 December 2026 # 42 days
What does (date(2026, 3, 1) - date(2026, 2, 1)).days return?
Summary
date,datetimeandtimedeltafrom thedatetimemodule cover most needs.- Add or subtract a
timedeltato move a date, and subtract two dates to get the gap. strftime()turns a date into text, andstrptime()reads text into a date.fromisoformat()andisoformat()handle the standard ISO 8601 format.- Store times in UTC and convert to local time only for display.
You have finished the module on text and data. Next, you will set up Python projects the way professionals do.
Finished reading? Tick it off to track your progress.