Velamuvi AIAcademy

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
In this lesson you will learn
  • 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:

TypeHoldsExample
datea calendar day17 November 2026
datetimea day and a time17 November 2026, 09:30
timedeltaa length of time3 days, 4 hours

Creating dates

Try it yourselfPython
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().

Try it yourselfPython
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.

Try it yourselfPython
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.

Try it yourselfPython
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.

Try it yourselfPython
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 %.

Try it yourselfPython
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"))
CodeMeaningExample
%d %m %Yday, month, four-digit year17 11 2026
%B %bmonth name, short month nameNovember Nov
%A %aweekday, short weekdayTuesday Tue
%H %Mhour (24-hour clock), minute14 05
%I %phour (12-hour clock), AM or PM02 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.

Try it yourselfPython
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().

Try it yourselfPython
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.

Try it yourselfPython
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

Practice exercise

Work out when a six-week course ends, and how long it lasts.

Your solutionPython
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
Knowledge check

What does (date(2026, 3, 1) - date(2026, 2, 1)).days return?

Summary

  • date, datetime and timedelta from the datetime module cover most needs.
  • Add or subtract a timedelta to move a date, and subtract two dates to get the gap.
  • strftime() turns a date into text, and strptime() reads text into a date.
  • fromisoformat() and isoformat() 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.