How to get week number in Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Getting a week number in Python is easy once you decide which week-numbering system you actually need. The main source of confusion is that ISO weeks, Sunday-based weeks, and Monday-based weeks do not always agree, especially around New Year.
Use isocalendar() for ISO week numbers
If you want the ISO-8601 week number, use date.isocalendar(). This is usually the safest default for business reporting and calendar-style logic.
In current Python versions, isocalendar() returns a named tuple-like object, so iso.week is clearer than indexing into position 1.
ISO weeks follow these rules:
- Monday is the first day of the week
- week
1is the week containing the first Thursday of the year
That means a date in early January can belong to the final ISO week of the previous ISO year.
Compare %U and %W in strftime
Python also exposes week numbers through strftime, but these formats use different conventions:
- '
%Ucounts weeks starting on Sunday' - '
%Wcounts weeks starting on Monday'
Example:
These results can differ. That does not mean one is wrong. It means they answer different calendar questions.
Also note that %U and %W return strings such as "00" or "01". Convert them if you need integers:
Boundary dates are where the differences matter
Dates near the start and end of a year are the tricky ones.
A date can belong to calendar year 2026 while still belonging to ISO week year 2025. If the program groups data by week number, storing only the week and not the associated week year can create ambiguous data.
Build a helper and keep one rule everywhere
If your application uses week numbers repeatedly, wrap the chosen rule in a helper so the whole codebase stays consistent.
This is better than scattering %U, %W, and isocalendar() calls across different files with no explicit policy.
Pandas makes vectorized week extraction easy
If you are working with many dates in a DataFrame, Pandas can expose ISO week information efficiently.
This is especially useful in reporting pipelines, analytics, and time-series grouping.
Pick the week system that matches the domain
Use ISO weeks when:
- business reports require ISO-style week numbering
- consistency across countries matters
- the surrounding system already uses ISO calendars
Use %U or %W only when your domain specifically wants those rules. The problem is not choosing the "wrong" one in the abstract. The problem is mixing several definitions in one application without realizing it.
Common Pitfalls
The biggest mistake is using the week number alone without its associated year system. Week 1 without the corresponding calendar rule and year is often ambiguous.
Another issue is assuming %U, %W, and ISO week numbers should always match. They do not, especially near New Year.
Developers also forget that strftime("%U") and strftime("%W") return strings. That can create subtle sorting or comparison bugs if they are treated as integers later.
Finally, always test dates at year boundaries if week numbers drive reporting or billing logic. Those boundary cases are where most bugs surface.
Summary
- Use
isocalendar().weekwhen you need ISO week numbers. - '
%Uand%Wfollow different rules and can disagree with ISO weeks.' - Dates near New Year can belong to a different ISO week year than their calendar year.
- Store both the week number and its associated year when grouping by week.
- Pick one week-numbering policy for the whole application and use it consistently.
Related reading
- How to get/set a pandas index column title or name?
- how to give the test size in stratified kfold sampling in python?
- How to graph grid scores from GridSearchCV?
- How to group dataframe rows into list in pandas groupby
- How to Guarantee Message delivery with Celery?
- How to handle connection issues with kafka using the python kafka library?
- How to handle errors with boto3?
- How to handle errors with boto3?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.