Get date from week number

PythonDatetimeStrptime

Python Problem Overview


Please what's wrong with my code:

import datetime
d = "2013-W26"
r = datetime.datetime.strptime(d, "%Y-W%W")
print(r)

Display "2013-01-01 00:00:00", Thanks.

Python Solutions


Solution 1 - Python

A week number is not enough to generate a date; you need a day of the week as well. Add a default:

import datetime
d = "2013-W26"
r = datetime.datetime.strptime(d + '-1', "%Y-W%W-%w")
print(r)

The -1 and -%w pattern tells the parser to pick the Monday in that week. This outputs:

2013-07-01 00:00:00

%W uses Monday as the first day of the week. While you can pick your own weekday, you may get unexpected results if you deviate from that.

See the strftime() and strptime() behaviour section in the documentation, footnote 4:

> When used with the strptime() method, %U and %W are only used in calculations when the day of the week and the year are specified.

Note, if your week number is a ISO week date, you'll want to use %G-W%V-%u instead! Those directives require Python 3.6 or newer.

Solution 2 - Python

To complete the other answers - if you are using ISO week numbers, this string is appropriate (to get the Monday of a given ISO week number):

import datetime
d = '2013-W26'
r = datetime.datetime.strptime(d + '-1', '%G-W%V-%u')
print(r)

%G, %V, %u are ISO equivalents of %Y, %W, %w, so this outputs:

2013-06-24 00:00:00

Availabe in Python 3.6+; from docs.

Solution 3 - Python

In Python 3.8 there is the handy datetime.date.fromisocalendar:

>>> from datetime import date
>>> date.fromisocalendar(2020, 1, 1)  # (year, week, day of week)
datetime.date(2019, 12, 30, 0, 0)

In older Python versions (3.7-) the calculation can use the information from datetime.date.isocalendar to figure out the week ISO8601 compliant weeks:

from datetime import date, timedelta

def monday_of_calenderweek(year, week):
    first = date(year, 1, 1)
    base = 1 if first.isocalendar()[1] == 1 else 8
    return first + timedelta(days=base - first.isocalendar()[2] + 7 * (week - 1))

Both works also with datetime.datetime.

Solution 4 - Python

import datetime
res = datetime.datetime.strptime("2018 W30 w1", "%Y %W w%w")
print res

Adding of 1 as week day will yield exact current week start. Adding of timedelta(days=6) will gives you the week end.

datetime.datetime(2018, 7, 23)

Solution 5 - Python

In case you have the yearly number of week, just add the number of weeks to the first day of the year.

>>> import datetime
>>> from dateutil.relativedelta import relativedelta

>>> week = 40
>>> year = 2019
>>> date = datetime.date(year,1,1)+relativedelta(weeks=+week)
>>> date
datetime.date(2019, 10, 8)

Solution 6 - Python

If anyone is looking for a simple function that returns all working days (Mo-Fr) dates from a week number consider this (based on accepted answer)

import datetime

def weeknum_to_dates(weeknum):
    return [datetime.datetime.strptime("2021-W"+ str(weeknum) + str(x), "%Y-W%W-%w").strftime('%d.%m.%Y') for x in range(-5,0)]

weeknum_to_dates(37)

Output:

['17.09.2021', '16.09.2021', '15.09.2021', '14.09.2021', '13.09.2021']

Solution 7 - Python

Here's a handy function including the issue with zero-week.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAli SAID OMARView Question on Stackoverflow
Solution 1 - PythonMartijn PietersView Answer on Stackoverflow
Solution 2 - PythonLuka KikeljView Answer on Stackoverflow
Solution 3 - PythonJens W. KleinView Answer on Stackoverflow
Solution 4 - PythonSenthilkumar CView Answer on Stackoverflow
Solution 5 - PythondimosbeleView Answer on Stackoverflow
Solution 6 - Pythondo-meView Answer on Stackoverflow
Solution 7 - PythonRonView Answer on Stackoverflow