{"id":46256,"date":"2023-03-31T07:58:05","date_gmt":"2023-03-31T07:58:05","guid":{"rendered":"https:\/\/www.askpython.com\/?p=46256"},"modified":"2026-09-29T17:48:46","modified_gmt":"2026-09-29T17:48:46","slug":"get-week-numbers","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/get-week-numbers","title":{"rendered":"How to Get Week Numbers in Python"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Let us get familiar with an interesting approach to retrieving the week number in python. As we know Python has provided us with many useful and easy functions to get our desired output, one of them allows us to get the current week, specified week, or a range of week numbers via isocalendar() functions of the datetime module. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This can be helpful for any planning or management purpose, for example, payroll process, sales and marketing analysis, event planning, project management, academic planning, etc basically anywhere where weekly cycles are important.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Overview of <code>isocalendar()<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>isocalendar()<\/code> returns a tuple of ISO year, ISO week, and the ISO day of the given date. The <a href=\"https:\/\/en.wikipedia.org\/wiki\/ISO_week_date\" data-type=\"URL\" data-id=\"https:\/\/en.wikipedia.org\/wiki\/ISO_week_date\" target=\"_blank\" rel=\"noreferrer noopener\">ISO calendar<\/a> is a standard calendar system that follows the Gregorian calendar system and is widely used in international business, government, and other organizations. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>isocalendar()<\/code> method is used in scheduling and planning software which includes the calculation of dates and time periods according to international standards (across different countries and regions). The syntax is the following:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndatetime_object.isocalendar()\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><code>datetime_object<\/code> is a <code>datetime<\/code> an object which contains the date <\/li>\n\n\n\n<li>ISO year: 4-digit integer which depicts the ISO year coating the maximum weeks<\/li>\n\n\n\n<li>ISO week number: integer range within 1 &#8211; 53 representing the ISO week number. It begins on Monday by default.<\/li>\n\n\n\n<li>ISO day of the week: Integer range from 1 to 7 representing the ISO day number. Monday is assigned 1, Tuesday 2, and so on.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Code Implementation to Get Week Numbers in Python<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s now get right into the different examples of how you can get the week numbers in Python.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1: Week Number for the Current Year<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport datetime\n\ntoday = datetime.datetime.today()\nweek_num = today.isocalendar()&#x5B;1]\n\nprint(&quot;Week number:&quot;,week_num,&quot;weeks&quot;)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">We import <code>datetime<\/code> module which contains functions like <code>isocalendar()<\/code> . We calculate the current date via <code>datetime.datetime.today()<\/code> and retrieve the count of weeks through <code>isocalendar()<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong>  <code>isocalendar(<\/code>)[1] returns the ISO week number for the current date. Each weekday is assigned a number 1-Monday 2-Tuesday and so on, by default, isocalendar() considers Monday the first day. This can be altered by changing the integer argument.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"266\" height=\"31\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/current_week.png\" alt=\"Current Week\" class=\"wp-image-46260\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The number of weeks is counted from the current year ie January 2023 thus 9 weeks.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: Week Number for a Different Year<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport datetime\n\ndate_str = &#039;2022-05-01&#039;\ndate = datetime.datetime.strptime(date_str, &#039;%Y-%m-%d&#039;)\nweek_num = date.isocalendar()&#x5B;1]\n\nprint(&quot;Week number for&quot;,date_str,&quot;:&quot;,week_num)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The above code uses the <code>strptime()<\/code> method from the <code>datetime<\/code> module to convert the string variables to datetime objects. The <code>strptime()<\/code> method takes two arguments: the string to be parsed as a date, and the format of the date in the string. In our case, the format is &#8216;%Y-%m-%d&#8217;, which represents year-month-day. The output will display the week number for 1st May 2022.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"405\" height=\"37\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/different_year.png\" alt=\"Different Year\" class=\"wp-image-46261\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/different_year.png 405w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/different_year-300x27.png 300w\" sizes=\"auto, (max-width: 405px) 100vw, 405px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Example 3: Week Number for a Given Range<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport datetime\n\nstart_date_str = &#039;2023-02-01&#039;\nend_date_str = &#039;2023-03-15&#039;\n\nstart_date = datetime.datetime.strptime(start_date_str, &#039;%Y-%m-%d&#039;)\nend_date = datetime.datetime.strptime(end_date_str, &#039;%Y-%m-%d&#039;)\n\nprint(&quot;Number of weeks from&quot;,start_date_str,&quot;to&quot;,end_date_str)\n\nfor date in (start_date + datetime.timedelta(n) for n in range((end_date - start_date).days + 1)):\n    week_num = date.isocalendar()&#x5B;1]\n    print(date.strftime(&#039;%Y-%m-%d&#039;),&quot;is in week number&quot;,week_num)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">We mention the start and end dates we wish to count weeks off.  We then create a <code>for<\/code> loop that iterates over a range of days between the start and end dates, using the <code>timedelta()<\/code> method to increment the date by one day each time. For each date in the loop, the code uses the <code>isocalendar()<\/code> method to get the ISO calendar week number. And later print every date with its week number.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/range_week.png\" alt=\"Range Week\" class=\"wp-image-46262\" width=\"361\" height=\"558\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/range_week.png 292w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/range_week-194x300.png 194w\" sizes=\"auto, (max-width: 361px) 100vw, 361px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The output will display the week numbers for each day between 1st February 2023 and 1st March 2023.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, we explored how to get week numbers in Python using the isocalendar() method of the datetime module. This method simplifies calculating week numbers for various dates or ranges. Keep in mind that isocalendar() considers Monday as the first day with an ISO number of 1 and Sunday with an ISO number of 7. Expand your Python knowledge by exploring other articles on AskPython and related resources.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>You can browse more interesting articles:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.askpython.com\/python\/built-in-methods\/mocking-in-python-unittestmock\">Mocking in Python Using Unittest.mock<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.askpython.com\/python-modules\/tinyhtml-module\">Python Tinyhtml &#8211; Create HTML Documents With Python<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Let us get familiar with an interesting approach to retrieving the week number in python. As we know Python has provided us with many useful and easy functions to get our desired output, one of them allows us to get the current week, specified week, or a range of week numbers via isocalendar() functions of [&hellip;]<\/p>\n","protected":false},"author":56,"featured_media":70236,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-46256","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/46256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/56"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=46256"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/46256\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/70236"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=46256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=46256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=46256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}