Python Datetime Methods

About This Index
This page is the Python Datetime Methods index for the tutorial track. Each row links to a full guide with syntax, parameters, return value, three runnable examples, and FAQs. Every example uses fixed dates and fixed UTC offsets, so the output you see is the output you get, whatever your clock or time zone says.
Learn naive vs aware objects and format codes on the Datetime hub. Use this page when you need a specific method such as strftime(), fromisoformat(), or astimezone().
The Class column tells you where a method lives: datetime.datetime (date plus time), datetime.date (calendar day), datetime.time (clock time), datetime.timedelta (duration), or datetime.tzinfo (time zone rules). utcnow() and utcfromtimestamp() are deprecated since Python 3.12.
How to Call a Datetime Method
Classmethods are called on the class and build a new object; instance methods are called on an object you already have:
from datetime import datetime, timedelta, timezone
moment = datetime(2024, 3, 15, 10, 30) # constructor
parsed = datetime.strptime("15/03/2024", "%d/%m/%Y") # classmethod
print(moment.strftime("%d %b %Y")) # 15 Mar 2024
print(moment.weekday()) # 4 (Friday)
print(parsed.date()) # 2024-03-15
print(timedelta(hours=2).total_seconds()) # 7200.0
print(moment.replace(tzinfo=timezone.utc).isoformat()) Python Datetime Methods Table
All method guides in this Python tutorial series (29 tutorials), grouped by purpose.
Create date and datetime objects
10 methodsClass methods that build a new object: the current moment, an epoch timestamp, a day number, an ISO 8601 string, an ISO week, a custom-format string, or a date plus a time.
| Method | Class | Description | Tutorial |
|---|---|---|---|
now() | datetime.datetime | Classmethod: the current date and time, naive local or aware with tz. | Try it |
today() | datetime.date (also datetime) | Classmethod: the current local date (date.today) or naive local date and time. | Try it |
utcnow() | datetime.datetime | Deprecated classmethod: current UTC time as a naive datetime; use now(timezone.utc). | Try it |
fromtimestamp() | datetime.datetime (also date) | Classmethod: turn a POSIX timestamp (seconds since 1970 UTC) into a datetime. | Try it |
utcfromtimestamp() | datetime.datetime | Deprecated classmethod: timestamp to naive UTC datetime; use fromtimestamp(ts, tz=timezone.utc). | Try it |
fromordinal() | datetime.date (also datetime) | Classmethod: build a date from a proleptic Gregorian day number (1 = 0001-01-01). | Try it |
fromisoformat() | datetime.datetime (also date, time) | Classmethod: parse ISO 8601 text such as 2024-03-15T10:30:00+05:30. | Try it |
fromisocalendar() | datetime.date (also datetime) | Classmethod: build a date from an ISO year, week number, and weekday. | Try it |
strptime() | datetime.datetime | Classmethod: parse a string into a datetime using format codes like %Y-%m-%d. | Try it |
combine() | datetime.datetime | Classmethod: join a date and a time into one datetime, with an optional tzinfo. | Try it |
Format and convert
10 methodsTurn a date or datetime into text, a time tuple, ISO week numbers, a weekday number, a day number, or a POSIX timestamp.
| Method | Class | Description | Tutorial |
|---|---|---|---|
strftime() | datetime.datetime (also date, time) | Format a date or datetime as text using % codes such as %Y-%m-%d %H:%M. | Try it |
isoformat() | datetime.datetime (also date, time) | Return ISO 8601 text such as 2024-03-15T10:30:45+05:30, with sep and timespec options. | Try it |
ctime() | datetime.datetime (also date) | Return a fixed C-style string such as 'Fri Mar 15 10:30:45 2024'. | Try it |
timetuple() | datetime.datetime (also date) | Return a time.struct_time with year, day of year, weekday, and DST flag. | Try it |
utctimetuple() | datetime.datetime | Return a struct_time in UTC: aware values are converted, the DST flag is 0. | Try it |
isocalendar() | datetime.date (also datetime) | Return the ISO 8601 (year, week, weekday) for a date. | Try it |
isoweekday() | datetime.date (also datetime) | Return the day of the week as 1 (Monday) to 7 (Sunday). | Try it |
weekday() | datetime.date (also datetime) | Return the day of the week as 0 (Monday) to 6 (Sunday). | Try it |
toordinal() | datetime.date (also datetime) | Return the proleptic Gregorian day number, where 0001-01-01 is day 1. | Try it |
timestamp() | datetime.datetime | Return the POSIX timestamp (seconds since 1970 UTC) as a float. | Try it |
Extract and replace parts
4 methodsSplit a datetime into its date and time parts, keep or drop the time zone, and build a copy with some fields changed.
| Method | Class | Description | Tutorial |
|---|---|---|---|
date() | datetime.datetime | Return the date part of a datetime as a date object. | Try it |
time() | datetime.datetime | Return the clock part of a datetime as a naive time object. | Try it |
timetz() | datetime.datetime | Return the clock part of a datetime as a time object that keeps its tzinfo. | Try it |
replace() | datetime.datetime (also date, time) | Return a copy with some fields changed, such as year, hour, or tzinfo. | Try it |
Time zones and offsets
4 methodsConvert an aware datetime to another zone and ask its tzinfo for the UTC offset, the daylight saving adjustment, and the zone name.
| Method | Class | Description | Tutorial |
|---|---|---|---|
astimezone() | datetime.datetime | Convert an aware datetime to another time zone, keeping the same instant. | Try it |
utcoffset() | datetime.datetime (also time, tzinfo) | Return the offset from UTC as a timedelta, or None for naive values. | Try it |
dst() | datetime.datetime (also time, tzinfo) | Return the daylight saving adjustment as a timedelta, or None. | Try it |
tzname() | datetime.datetime (also time, tzinfo) | Return the time zone name, such as 'UTC' or 'UTC+05:30', or None. | Try it |
Durations
1 methodWork with timedelta objects, the result of subtracting two dates or datetimes.
| Method | Class | Description | Tutorial |
|---|---|---|---|
total_seconds() | datetime.timedelta | Return the whole length of a timedelta in seconds, as a float. | Try it |
Frequently Asked Questions
Start with astimezone()
Open the first guide in the list and work your way through the datetime methods.
14 people found this page helpful
