Python datetime.isocalendar() Method

Musfirah Waseem Jan 30, 2023
  1. Syntax of Python datetime.isocalendar() Method
  2. Example 1: Use the datetime.isocalendar() Method in Python
  3. Example 2: Specify a Date in the datetime.isocalendar() Method
  4. Example 3: Shorter Syntax of the datetime.isocalendar() Method
Python datetime.isocalendar() Method

Python datetime.isocalendar() method is an efficient way of finding the ISO year, ISO week number, and ISO weekday.

Syntax of Python datetime.isocalendar() Method

datetime.isocalendar()

Parameters

This method doesn’t accept any parameter.

Return

The return time of this method is a tuple representing the ISO year, week number, and weekday.

  1. ISO standard 8601 and ISO standard 2015 state that Thursday is the middle day of the week.
  2. ISO years always start with a Monday.
  3. ISO years are either 52 full weeks or 53 full weeks.
  4. ISO years do not have any fractional weeks.

Example 1: Use the datetime.isocalendar() Method in Python

from datetime import date

date = date.today()

print("Today's date is ", date)

print("The tuple is ", date.isocalendar())

Output:

Today's date is  2022-08-31
The tuple is  (2022, 35, 3)

The above code displays the year, week number, and weekday for today’s datetime object.

Example 2: Specify a Date in the datetime.isocalendar() Method

import datetime

date = datetime.date(1776, 7, 4)

print("America got its independence on {}".format(date))

calendar = date.isocalendar()

print("America's independence in isocalendar format is {}".format(calendar))

Output:

America got its independence on 1776-07-04
America's independence in isocalendar format is (1776, 27, 4)

Like the above code, you can mention any specific date you want to find the year, week number, and weekday.

Example 3: Shorter Syntax of the datetime.isocalendar() Method

import datetime

print(datetime.date(2022, 2, 19).isocalendar())

Output:

(2022, 7, 6)

You can use the dot notation to access the isocalendar() method of any datetime object.

Musfirah Waseem avatar Musfirah Waseem avatar

Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.

LinkedIn

Related Article - Python DateTime