Python datetime.isocalendar() Method
-
Syntax of Python
datetime.isocalendar()
Method -
Example 1: Use the
datetime.isocalendar()
Method in Python -
Example 2: Specify a Date in the
datetime.isocalendar()
Method -
Example 3: Shorter Syntax of the
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.
- ISO standard 8601 and ISO standard 2015 state that
Thursday
is the middle day of the week. - ISO years always start with a
Monday
. - ISO years are either 52 full weeks or 53 full weeks.
- 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 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