0

This question is related to Get date from week number, and is possibly a duplicate of the latter, however, I think what is suggested in the accepted answer to that question does not really work.

In [6]: datetime.datetime.strptime('2019-18-1', "%Y-%W-%w")
Out[6]: datetime.datetime(2019, 5, 6, 0, 0)

Notice how it returns Monday 2019-5-6. However, according to the calendar (I use http://whatweekisit.org for reference), 2019-5-6 the Monday of week 19.

Similarly, the example provided in the original question:

In [7]: datetime.datetime.strptime('2013-26-1', "%Y-%W-%w")
Out[7]: datetime.datetime(2013, 7, 1, 0, 0)

According to http://whatweekisit.org/calendar-2013.html 2013-7-1 is the Monday of week 27.

Also

In [8]: datetime.datetime.strptime('2019-18-1', "%Y-%W-%w").isocalendar()[1]
Out[8]: 19

Notice how I give week 18 to strptime, and get week 19 back from isocalendar.

I am completely lost and would very much appreciate if someone could explain what is going on here. My original goal though is to get week start date from week number.

Andrii Yurchuk
  • 3,090
  • 6
  • 29
  • 40
  • I sounds to me like you are explaining an off by 1 error. If so, couldn't you just subtract one from the result? – Reedinationer May 10 '19 at 18:05
  • @Reedinationer I could of course, but I would like to understand why exactly it works like this. I doubt that it is a bug in Python. – Andrii Yurchuk May 10 '19 at 18:07
  • 1
    What weeknumber is used depends on certain cultural things - for one which day starts a week? Sunday or monday? sometimes week1 starts in the last year. from the docu of isocalender: https://docs.python.org/3/library/datetime.html#datetime.date.isocalendar **For example, 2004 begins on a Thursday, so the first week of ISO year 2004 begins on Monday, 29 Dec 2003 and ends on Sunday, 4 Jan 2004, so that date(2003, 12, 29).isocalendar() == (2004, 1, 1) and date(2004, 1, 4).isocalendar() == (2004, 1, 7).** – Patrick Artner May 10 '19 at 18:07

2 Answers2

2

Based off of my testing, datetime does not consider the first week of 2019 (i.e. Jan 1-Jan 6) as week 1 because it isn't a full week; December 31st, 2018 is part of the week but is not in 2019. I suppose you'll have to accomodate for that by checking the output of datetime.datetime.strptime('year-1-1', "%Y-%W-%w") == datetime.datetime.strptime('year-0-1', "%Y-%W-%w"). If false, subtract 1.

2018 is an example of a year where datetime does return the same value as isocalendar because the first Monday of the year is Jan 1.

101arrowz
  • 1,825
  • 7
  • 15
0

From the isocalendar docs:

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.


On the other hand, strptime starts from the first full week, in fact, since 2019 starts from Tuesday, they start from different weeks:

import datetime as dt
strp_first = dt.datetime.strptime('2019-1-1', "%Y-%W-%w")

>>> print(strp_first)
2019-01-07 00:00:00

>>> print(strp_first.isocalendar()[1])
2

While in 2021, which starts from Friday:

strp_first = dt.datetime.strptime('2021-1-1', "%Y-%W-%w")

>>> print(strp_first)
2021-01-04 00:00:00

>>> print(strp_first.isocalendar()[1])
1
Lante Dellarovere
  • 1,838
  • 2
  • 7
  • 10