For today’s Python Tip, we will see how to get the week number of the year. To get the week number for a specific date or the current date, we can use the date.isocalendar() method in the datetime module. isocalendar returns a 3 valued tuple with week number being the second. Here is an example.
import datetime
week_number = datetime.date(2019, 8, 2).isocalendar()[1]
print("\nWeek number: ", week_number, "\n")
![Getting week number from a given date in Python](https://www.mytecbits.com/wp-content/uploads/python-week-number-776x425.jpg)
More Tips
Reference
- More about date.isocalendar() at Python docs.