Getting week number from a given date in Python

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

More Tips

Reference


Leave your thoughts...

This site uses Akismet to reduce spam. Learn how your comment data is processed.