Get date from week number Get date from week number python python

Get date from week number


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

import datetimed = "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.


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 datetimed = '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.


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, timedeltadef 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.