I summarize the discussion to two steps:
- Convert the raw format to a
datetime
object. - Use the function of a
datetime
object or adate
object to calculate the week number.
Warm up
from datetime import datetime, date, time
d = date[2005, 7, 14]
t = time[12, 30]
dt = datetime.combine[d, t]
print[dt]
1st step
To manually generate a datetime
object, we can use datetime.datetime[2017,5,3]
or datetime.datetime.now[]
.
But in reality, we usually need to parse an existing string. we can use strptime
function, such as datetime.strptime['2017-5-3','%Y-%m-%d']
in which you
have to specific the format. Detail of different format code can be found in the official documentation.
Alternatively, a more convenient way is to use dateparse module. Examples are dateparser.parse['16 Jun 2010']
, dateparser.parse['12/2/12']
or dateparser.parse['2017-5-3']
The above two approaches will return a datetime
object.
2nd step
Use the obtained datetime
object to call strptime[format]
. For example,
python
dt = datetime.strptime['2017-01-1','%Y-%m-%d'] # return a datetime object. This day is Sunday
print[dt.strftime["%W"]] # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print[dt.strftime["%U"]] # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print[dt.strftime["%V"]] # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
It's very tricky to decide which format to use. A better way is to get a date
object to call isocalendar[]
. For example,
python
dt = datetime.strptime['2017-01-1','%Y-%m-%d'] # return a datetime object
d = dt.date[] # convert to a date object. equivalent to d = date[2017,1,1], but date.strptime[] don't have the parse function
year, week, weekday = d.isocalendar[]
print[year, week, weekday] # [2016,52,7] in the ISO standard
In reality, you will be more likely to use date.isocalendar[]
to prepare a weekly report, especially in the Christmas-New Year
shopping season.
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.