datetime ISO week says first and last day of year are both in same week

Viewed 424

I'm running Python 3.8.3 and I found something weird about the ISO Week format (%V) :

The first day and the last day of 2019 are both in week 1.

from datetime import date
print(date(2019, 1, 1).strftime('%Y-W%V'))
print(date(2019, 12, 29).strftime('%Y-W%V'))
print(date(2019, 12, 31).strftime('%Y-W%V'))

Output:

2019-W01  
2019-W52  
2019-W01

Why does it behave like that?

1 Answers

It is fully correct.

As you see in your dates, all of them are in 2019, so it is correct to get 2019 with %Y.

Week number is defined by ISO, and so one week could be considered in previous or in next year.

You need to use %G to get year of the week number (%V).

Related