Return Current Number of Week in Python (Example Code)

On this page you’ll learn how to find the current week number in Python programming.

Creation of Example Data

First, we have to load the datetime module to Python:

import datetime                   # Import datetime

The following data will be used for the tutorial:

date_x = datetime.date.today()    # Print current local date
print(date_x)
# 2022-06-13

Example: Get Actual Number of Week

To show the current weeknumber, we can apply the isocalendar function, which returns as a tuple of the following information: year[0], weeknumber[1], and day of week[2].

Since we want to know only the number of the week, we use the isocalendar function followed by the index [1]:

wn = date_x.isocalendar()[1]      # Applying isocalendar() function
print(wn)                         # Print week number
# 24

 

Matthias Bäuerlen Python Programmer

Note: This article was created in collaboration with Matthias Bäuerlen. Matthias is a programmer who helps to create tutorials on the Python programming language. You might find more info about Matthias and his other articles on his profile page.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top