Datetime#

This page describes Python features for working with date and time objects. Check the relevant page in the documentation.

from datetime import date, time, datetime, timezone, tzinfo

String conversions#

It turns out that it’s quite typical to transform strings to datetime data types and vice versa, so this question deserves separate consideration. Check corresponding section in official documentation.

Use datetime.strptime to convert string to date and the strftime method of datetime.date, datetime.time, or datetime.datetime objects.

You must specify a format string that indicates the format to convert to or convert from. You can specify different components of date or time (such as days, hours, etc.) using special symbols that begin with the % symbol. Look for a full list of possible components in the corresponding section of the official documentation.


Consider some examples of format strings. The following cell prints outputs for a set of format strings for typical formats used in different contexts.

obj = datetime(2000, 10, 20, 12, 33, 17, 3333)
formats = [
    "%Y-%m-%d %H-%M-%S.%f",
    "%d.%m.%Y",
    "%Y/%d/%m"
]

for format in formats:
    print(format + " -> " + obj.strftime(format))
%Y-%m-%d %H-%M-%S.%f -> 2000-10-20 12-33-17.003333
%d.%m.%Y -> 20.10.2000
%Y/%d/%m -> 2000/20/10

An example of conversion from string to datetime: the same string with different format strings produces different datetime objects.

datetime.strptime("2000-10-12", "%Y-%m-%d"),
datetime.strptime("2000-10-12", "%Y-%d-%m")
datetime.datetime(2000, 12, 10, 0, 0)

Timezones#

The datetime.datetime objects are categorized into two groups:

  • Naive doesn’t contain the information about timezone.

  • Aware contains the attribute tzinfo which contains the information about the timezone.


The following cell creates the datetime with pure now call.

naive = datetime.now()
print(naive, naive.tzinfo)
2026-01-27 15:37:28.214794 None

As a result, there is time from my system, and None value of the tzinfo attribute.

The same call but with specification of the tinezone.utc.

aware = datetime.now(tz=timezone.utc)
print(aware, aware.tzinfo)
2026-01-27 14:37:28.982123+00:00 UTC

The time is converted to the specified time zone, and zinfo takes corresponding value.

Convertation#

You can:

  • Convert the date to specific timezone: with astimezone method.

  • Just update the tzinfo attribute of datetime object: with general replace method.


The following cell defines the utc datetime object. The astimezone method is used to convert it to the Minsk timezone.

from zoneinfo import ZoneInfo

now = datetime.now(tz=timezone.utc)
now, now.astimezone(ZoneInfo("Europe/Minsk"))
(datetime.datetime(2026, 1, 27, 14, 47, 16, 663930, tzinfo=datetime.timezone.utc),
 datetime.datetime(2026, 1, 27, 17, 47, 16, 663930, tzinfo=zoneinfo.ZoneInfo(key='Europe/Minsk')))

It’s not just thet tzinfo attribute of the result object that has changed, but the numbers as well.

The following cell shows an alternative option: updating only the tzinfo attribute of the object. The numbers save their values.

now.replace(tzinfo=ZoneInfo("Europe/Minsk"))
datetime.datetime(2026, 1, 27, 14, 47, 16, 663930, tzinfo=zoneinfo.ZoneInfo(key='Europe/Minsk'))