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")
More Tips
Reference
- More about date.isocalendar() at Python docs.