datetime

Dates and times: creating, formatting, parsing from text, and calculations on time intervals.

On this page

Example

#
Python
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)
Resultsaved result, you can check it
2026-09-28 10:30:00
05.10.2026, at 10:30
7
True

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

#
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, raises ValueError.
  • hour, minute, second

    int, default 0

    The hour, minute, and second.
  • tzinfo

    time zone, optional

    E.g. timezone.utc or ZoneInfo("Europe/Warsaw"). Without it, the date has no time zone attached.

More examples

#
Parsing a date from text
Python
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())
Resultsaved result, you can check it
2026-10-15 18:00:00
2026-10-15T18:00:00
2012 3

weekday() numbers the days from Monday (0) to Sunday (6), so 3 means Thursday.

How many days until the exam?
Python
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"))
Result
Click Run to see the result.

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() and fromisoformat() 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

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.