Get Current Week Number in Python (Example)
In this tutorial, I’ll demonstrate how to return the current number of week in the Python programming language.
Table of contents:
Sound good? Let’s just jump right in!
Example Data & Software Libraries
First, we need to import the datetime module.
import datetime # Import datetime module in Python
Next, we’ll also need to create some example data:
my_date = datetime.date.today() # Show actual date print(my_date) # 2022-06-13
The previous outputted date will be used for this tutorial.
Example: Get the Current Week Number
To return the week number of a date, we can use the isocalendar function.
This function returns the datetime object as a tuple containing the following information: year, number of week, and day of the week.
year, week_num, day_of_week = my_date.isocalendar() # Using isocalendar() function print("Week #" + str(week_num)) # Showing week number # Week #24
Consider the Python code above. We can use the str() function, to return the data we want to extract, so in our case str(week_num).
Video, Further Resources & Summary
Do you want to know more about the returning of the current week number? Then I recommend having a look at the following video on my YouTube channel. In the video, I illustrate the Python programming syntax of this article:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you might want to read the related articles on my website. You can find some posts below.
- Get Day of Week from datetime in Python – Number & Name (3 Examples)
- Get Week Number of datetime in Python (Example)
- Subtract Days, Months & Years from datetime Object in Python (3 Examples)
- Add Days, Months & Years to datetime Object in Python (3 Examples)
- Introduction to Python Programming
In this article, I have illustrated how to find the current week number in Python. Don’t hesitate to tell me about it in the comments, in case you have additional questions or comments. Furthermore, please subscribe to my email newsletter in order to receive regular updates on the newest articles.
This page was created in collaboration with Matthias Bäuerlen. Have a look at Matthias’ author page to get more information about his professional background, a list of all his tutorials, as well as an overview on his other tasks on Statistics Globe.