Арифметика дат

Простые типы / Дата и время

from datetime import datetime, timedelta
# pip install python-dateutil
from dateutil.relativedelta import relativedelta

# A fixed moment: the output stays the same
moment = datetime(2027, 1, 1, 22, 13, 20)
yesterday = moment - timedelta(days=1)
tomorrow = moment + timedelta(days=1)
in_one_hour = moment + timedelta(hours=1)

# timedelta has no months and years, relativedelta has
next_month = moment + relativedelta(months=1)
next_year = moment + relativedelta(years=1)

# The day is cut to the month end, no move to March
from_january31 = datetime(2027, 1, 31) + relativedelta(months=1)

print(f"{moment = }")
print(f"{yesterday = }")
print(f"{tomorrow = }")
print(f"{in_one_hour = }")
print(f"{next_month = }")
print(f"{next_year = }")
print(f"{from_january31 = }")