To get the week number from a date or datetime object in Python, the easiest way is to use the Python isocalendar() function.

import datetime

print(datetime.date(2022, 6, 20).isocalendar()[1])

#Output:
25

The Python datetime strftime() function also can be helpful to get the week number from the date in Python, depending on which calendar you want to use.

import datetime

dt = datetime.date(2022, 1, 2)  #Date is January 2nd (Sunday), 2022, year starts with Saturday

print(dt.strftime("%W"))
print(dt.strftime("%U"))
print(dt.strftime("%V"))

#Output:
'00'; Monday is considered first day of week, Sunday is the last day of the week which started in the previous year
'01'; Sunday is considered first day of week
'52'; ISO week number; result is '52' since there is no Thursday in this year's part of the week

When working with date and datetime variables in Python, the ability to easily be able to get different pieces of information about the dates is valuable.

One such piece of information is the week of the year.

There are a few different ways to get the week number of the year in Python.

The easiest way to get the week number is to use the Python datetime isocalendar() function.

The isocalendar() function returns a tuple object with three components: year, week and weekday.

The ISO calendar is a widely used variant of the Gregorian calendar.

The ISO year consists of 52 or 53 full weeks, and where a week starts on a Monday and ends on a Sunday. The first week of an ISO year is the first (Gregorian) calendar week of a year containing a Thursday. This is called week number 1, and the ISO year of that Thursday is the same as its Gregorian year.

Below is an example of how you can get the week number from a date using Python.

import datetime

print(datetime.date(2022, 6, 20).isocalendar()[1])

#Output:
25

If you are using Python 3.9+, then you can access the ‘week’ attribute from isocalendar().

import datetime

print(datetime.date(2022, 6, 20).isocalendar().week)

#Output:
25

Using strftime() to Get Week Number from Date in Python

Another way you can get the week number from a date variable in Python is with the strftime() function.

The strftime() function allows you to format dates with different date formats.

You can pass “%W”, “%U”, or “%V” to strftime() to get the number of the week according to three different calendars.

“%W” corresponds to the calendar where Monday is considered the first day of the week.

“%U” corresponds to the calendar where Sunday is considered the first day of the week.

“%V” corresponds to the ISO calendar.

Below is an example of how you can use strftime() to get the week number from a date in Python.

import datetime

print(datetime.date(2022, 6, 20).strftime("%W")) # Monday is considered first day of week
print(datetime.date(2022, 6, 20).strftime("%U")) # Sunday is considered first day of week
print(datetime.date(2022, 6, 20).strftime("%V")) # ISO week number

#Output:
25
25
25

Considering Calendar Differences When Finding Week Number in Python

Depending on which day of the week your calendar starts on, the week number can change depending on the date. For example, the first week of January can cause troubles for developers if they aren’t careful.

With the strftime() function, you can return the week number based on the three calendars we described above.

Depending on which calendar you are using, there can be differences of which week number you will get for certain dates.

Below shows the difference between the Gregorian calendar and ISO calendar return values for the first Sunday of the year.

import datetime

dt = datetime.date(2022, 1, 2)  #Date is January 2nd (Sunday), 2022, year starts with Saturday

print(dt.strftime("%W")) # Monday is considered first day of week, Sunday is the last day of the week which started in the previous year
print(dt.strftime("%U")) # Sunday is considered first day of week
print(dt.strftime("%V")) # ISO week number; result is '52' since there is no Thursday in this year's part of the week

#Output:
00 
01
52

Hopefully this article has been useful for you to use Python to get the week number from a date.

Categorized in:

Python,

Last Update: March 12, 2024