datetime module
The standard library module for dates and times as real values you can compare, add to and format, not as text.
In more detail
datetime.now() gives the current local date and time and date.today() the date. Subtracting two dates gives a timedelta, and adding a timedelta moves a date forward or back. Convert to and from text with strftime and strptime.
Example
from datetime import date
(date(2026, 12, 25) - date.today()).daysRelated terms
- timedelta — A datetime value representing a length of time, such as timedelta(days=7), which you add to or subtract from dates.
- strftime() — strftime turns a date into text using format codes such as %Y-%m-%d; strptime does the reverse, parsing text into a datetime.
- time zone — A region that shares the same offset from Coordinated Universal Time, including its daylight saving rules.
Taught in
- Dates, Times And Random — Hands-on Python: Complete Video Course & Book
- A Standard Library Tour — Hands-on Python: Complete Video Course & Book