datetime
Dates and times: creating, formatting, parsing from text, and calculations on time intervals.
Example
#from datetime import datetime, timedelta
start = datetime(2026, 9, 28, 10, 30)
deadline = start + timedelta(days=7)
print(start)
print(deadline.strftime("%d.%m.%Y, at %H:%M"))
print((deadline - start).days)
print(deadline > start)Definition and usage
#The datetime module contains classes for describing time: date (just a date), time (just a time of day), datetime (a date with a time), and timedelta (a time interval, for example 7 days or 90 minutes). They are most often imported directly: from datetime import datetime, timedelta.
The strftime() method turns a date into text according to a pattern, e.g. %d.%m.%Y gives β28.09.2026β, and datetime.strptime() works the other way, parsing a date from text. For the standard ISO format (2026-09-28T10:30:00), there are simpler methods, isoformat() and fromisoformat().
Dates are compared with the ordinary operators < and >, and adding a timedelta moves a date forward or back. Subtracting two dates gives a timedelta, from which you can read, for example, the number of days. The current moment is returned by datetime.now(), and for working with time zones there is the zoneinfo module.
Syntax
#from datetime import date, datetime, timedelta
datetime(year, month, day, hour=0, minute=0, second=0)
datetime.now()
moment.strftime(format)
datetime.strptime(text, format)
timedelta(days=0, hours=0, minutes=0)Parameters
#year
int
The year, e.g.2026.month
int 1-12
The month.day
int
The day of the month. A date that does not exist, e.g. April 31, raisesValueError.hour, minute, second
int, default 0
The hour, minute, and second.tzinfo
time zone, optional
E.g.timezone.utcorZoneInfo("Europe/Warsaw"). Without it, the date has no time zone attached.
More examples
#from datetime import date, datetime
exam = datetime.strptime("15.10.2026 18:00", "%d.%m.%Y %H:%M")
print(exam)
print(exam.isoformat())
birthday = date.fromisoformat("2012-05-03")
print(birthday.year, birthday.weekday())weekday() numbers the days from Monday (0) to Sunday (6), so 3 means Thursday.
from datetime import date, datetime
exam = date(2026, 12, 15)
days_left = (exam - date.today()).days
print(f"{days_left} days left until the exam")
print(datetime.now().strftime("%H:%M"))The result depends on when you run it. On September 28, 2026, the first line is β78 days left until the examβ, and the second shows the current time.
Good practices
#- Do not take dates apart by hand with
split().strptime()andfromisoformat()immediately check that the date is valid. - In f-strings you can format dates directly:
f"{deadline:%d.%m.%Y}". - For saving to JSON and databases, use the ISO format, and store the time of users from different countries in UTC:
datetime.now(timezone.utc).
Related entries
#- timePausing the program, timestamps, and precise measurement of code execution time.
- jsonConverts Python data to JSON text and back. The basis for saving data and communicating with APIs.
- f-stringsStrings with inserted expression values: f"You have {points} pts".
- str.format()Inserts values into the places marked with curly braces.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.