Let us get familiar with an interesting approach to retrieving the week number in python. As we know Python has provided us with many useful and easy functions to get our desired output, one of them allows us to get the current week, specified week, or a range of week numbers via isocalendar() functions of the datetime module.
This can be helpful for any planning or management purpose, for example, payroll process, sales and marketing analysis, event planning, project management, academic planning, etc basically anywhere where weekly cycles are important.
isocalendar() returns a tuple of ISO year, ISO week, and the ISO day of the given date. The ISO calendar is a standard calendar system that follows the Gregorian calendar system and is widely used in international business, government, and other organizations.
isocalendar() method is used in scheduling and planning software which includes the calculation of dates and time periods according to international standards (across different countries and regions). The syntax is the following:
datetimean object which contains the date
- ISO year: 4-digit integer which depicts the ISO year coating the maximum weeks
- ISO week number: integer range within 1 – 53 representing the ISO week number. It begins on Monday by default.
- ISO day of the week: Integer range from 1 to 7 representing the ISO day number. Monday is assigned 1, Tuesday 2, and so on.
Code Implementation to Get Week Numbers in Python
Let’s now get right into the different examples of how you can get the week numbers in Python.
Example 1: Week Number for the Current Year
import datetime today = datetime.datetime.today() week_num = today.isocalendar() print("Week number:",week_num,"weeks")
datetime module which contains functions like
isocalendar() . We calculate the current date via
datetime.datetime.today() and retrieve the count of weeks through
isocalendar() returns the ISO week number for the current date. Each weekday is assigned a number 1-Monday 2-Tuesday and so on, by default, isocalendar() considers Monday the first day. This can be altered by changing the integer argument.
The number of weeks is counted from the current year ie January 2023 thus 9 weeks.
Example 2: Week Number for a Different Year
import datetime date_str = '2022-05-01' date = datetime.datetime.strptime(date_str, '%Y-%m-%d') week_num = date.isocalendar() print("Week number for",date_str,":",week_num)
The above code uses the
strptime() method from the
datetime module to convert the string variables to datetime objects. The
strptime() method takes two arguments: the string to be parsed as a date, and the format of the date in the string. In our case, the format is ‘%Y-%m-%d’, which represents year-month-day. The output will display the week number for 1st May 2022.
Example 3: Week Number for a Given Range
import datetime start_date_str = '2023-02-01' end_date_str = '2023-03-15' start_date = datetime.datetime.strptime(start_date_str, '%Y-%m-%d') end_date = datetime.datetime.strptime(end_date_str, '%Y-%m-%d') print("Number of weeks from",start_date_str,"to",end_date_str) for date in (start_date + datetime.timedelta(n) for n in range((end_date - start_date).days + 1)): week_num = date.isocalendar() print(date.strftime('%Y-%m-%d'),"is in week number",week_num)
We mention the start and end dates we wish to count weeks off. We then create a
for loop that iterates over a range of days between the start and end dates, using the
timedelta() method to increment the date by one day each time. For each date in the loop, the code uses the
isocalendar() method to get the ISO calendar week number. And later print every date with its week number.
The output will display the week numbers for each day between 1st February 2023 and 1st March 2023.
In this article, we explored how to get week numbers in Python using the isocalendar() method of the datetime module. This method simplifies calculating week numbers for various dates or ranges. Keep in mind that isocalendar() considers Monday as the first day with an ISO number of 1 and Sunday with an ISO number of 7. Expand your Python knowledge by exploring other articles on AskPython and related resources.
You can browse more interesting articles: