• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Get Week Number from Date in Python

Get Week Number from Date in Python

June 20, 2022 Leave a Comment

To get the week number from a date or datetime object in Python, the easiest way is to use the Python isocalendar() function.

import datetime

print(datetime.date(2022, 6, 20).isocalendar()[1])

#Output:
25

The Python datetime strftime() function also can be helpful to get the week number from the date in Python, depending on which calendar you want to use.

import datetime

dt = datetime.date(2022, 1, 2)  #Date is January 2nd (Sunday), 2022, year starts with Saturday

print(dt.strftime("%W"))
print(dt.strftime("%U"))
print(dt.strftime("%V"))

#Output:
'00'; Monday is considered first day of week, Sunday is the last day of the week which started in the previous year
'01'; Sunday is considered first day of week
'52'; ISO week number; result is '52' since there is no Thursday in this year's part of the week

When working with date and datetime variables in Python, the ability to easily be able to get different pieces of information about the dates is valuable.

One such piece of information is the week of the year.

There are a few different ways to get the week number of the year in Python.

The easiest way to get the week number is to use the Python datetime isocalendar() function.

The isocalendar() function returns a tuple object with three components: year, week and weekday.

The ISO calendar is a widely used variant of the Gregorian calendar.

The ISO year consists of 52 or 53 full weeks, and where a week starts on a Monday and ends on a Sunday. The first week of an ISO year is the first (Gregorian) calendar week of a year containing a Thursday. This is called week number 1, and the ISO year of that Thursday is the same as its Gregorian year.

Below is an example of how you can get the week number from a date using Python.

import datetime

print(datetime.date(2022, 6, 20).isocalendar()[1])

#Output:
25

If you are using Python 3.9+, then you can access the ‘week’ attribute from isocalendar().

import datetime

print(datetime.date(2022, 6, 20).isocalendar().week)

#Output:
25

Using strftime() to Get Week Number from Date in Python

Another way you can get the week number from a date variable in Python is with the strftime() function.

The strftime() function allows you to format dates with different date formats.

You can pass “%W”, “%U”, or “%V” to strftime() to get the number of the week according to three different calendars.

“%W” corresponds to the calendar where Monday is considered the first day of the week.

“%U” corresponds to the calendar where Sunday is considered the first day of the week.

“%V” corresponds to the ISO calendar.

Below is an example of how you can use strftime() to get the week number from a date in Python.

import datetime

print(datetime.date(2022, 6, 20).strftime("%W")) # Monday is considered first day of week
print(datetime.date(2022, 6, 20).strftime("%U")) # Sunday is considered first day of week
print(datetime.date(2022, 6, 20).strftime("%V")) # ISO week number

#Output:
25
25
25

Considering Calendar Differences When Finding Week Number in Python

Depending on which day of the week your calendar starts on, the week number can change depending on the date. For example, the first week of January can cause troubles for developers if they aren’t careful.

With the strftime() function, you can return the week number based on the three calendars we described above.

Depending on which calendar you are using, there can be differences of which week number you will get for certain dates.

Below shows the difference between the Gregorian calendar and ISO calendar return values for the first Sunday of the year.

import datetime

dt = datetime.date(2022, 1, 2)  #Date is January 2nd (Sunday), 2022, year starts with Saturday

print(dt.strftime("%W")) # Monday is considered first day of week, Sunday is the last day of the week which started in the previous year
print(dt.strftime("%U")) # Sunday is considered first day of week
print(dt.strftime("%V")) # ISO week number; result is '52' since there is no Thursday in this year's part of the week

#Output:
00 
01
52

Hopefully this article has been useful for you to use Python to get the week number from a date.

Other Articles You'll Also Like:

  • 1.  How to Group By Columns and Find Sum in pandas DataFrame
  • 2.  islower Python – Check if All Letters in String Are Lowercase
  • 3.  Get All Substrings of a String in Python
  • 4.  pandas Absolute Value – Get Absolute Values in a Series or DataFrame
  • 5.  Divide Each Element in List by Scalar Value with Python
  • 6.  Get Length of File Using Python
  • 7.  Find First Occurrence in String of Character or Substring in Python
  • 8.  Using Python to Read Random Line from File
  • 9.  math.degrees() Python – How to Convert Radians to Degrees in Python
  • 10.  How to Check if String Contains Uppercase Letters in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy