Python datetime Module
Example
Create a date and show ISO string and weekday (0=Monday..6=Sunday):
import datetime
d = datetime.date(2025, 1, 1)
print(d.isoformat())
print(d.weekday())
Try it Yourself »
Definition and Usage
The datetime module supplies classes for manipulating dates and times.
Use it to handle dates, times, time zones, formatting/parsing, arithmetic, and comparisons.
Members
| Member | Description |
|---|---|
| date | Class for working with dates (year, month, day). |
| date.fromisoformat() | Create a date from an ISO 8601 string. |
| date.fromordinal() | Create a date from a proleptic Gregorian ordinal. |
| date.fromtimestamp() | Create a date from a POSIX timestamp. |
| date.today() | Return the current local date. |
| datetime | Class for working with dates and times together. |
| datetime.combine() | Create a datetime from a date and a time. |
| datetime.fromisoformat() | Create a datetime from an ISO 8601 string. |
| datetime.fromtimestamp() | Create a datetime from a POSIX timestamp (local time). |
| datetime.utcfromtimestamp() | Create a UTC datetime from a POSIX timestamp. |
| MAXYEAR | Maximum allowed year value. |
| MINYEAR | Minimum allowed year value. |
| time | Class for working with times (hour, minute, second, microsecond). |
| time.fromisoformat() | Create a time from an ISO 8601 string. |
| timedelta | Represents a duration, the difference between two dates or times. |
| timedelta.total_seconds() | Return the total number of seconds contained in the duration. |
| timezone.utc | UTC time zone singleton. |
| tzinfo | Abstract base class for time zone information objects. |