Python Glossary Русский

all Python 3datetimeterm

datetime

module

Dates, times of day, their combination and the spans between them: timedelta arithmetic, formatting and parsing strings, time zones.

Syntax

from datetime import date, datetime, timedelta

datetime is the module for calendar values: a date plus 30 days, the gap between two moments, output as "dd.mm.yyyy". For measuring durations and epoch seconds use time, and for the rules of a specific zone use zoneinfo. The main trap is naive objects without tzinfo: they cannot be compared with aware ones, and datetime.now() without an argument returns the machine's local time.

Examples

from datetime import date, timedelta
d = date(2024, 2, 28)
print(d + timedelta(days=2))  # → 2024-03-01 — 2024 год високосный
print(d.strftime('%d.%m.%Y'))  # → 28.02.2024

See also

Open in the full glossarydocs.python.orgReport an inaccuracy