Search code examples
pythondatetimeiso

Python datetime isocalendar giving wrong tuple


Here is my python code

import datetime
a = datetime.datetime(2012, 1, 1)
print(a.isocalendar())

and the output is

(2011, 52, 7)

The format for the output is a tuple containing year, weeknumber and weekday in respective order for the given date instance.

Why is this so? Shouldn't it be (2012, 1, 1), if not how can I convert it to an absolute weeknumber?


Solution

  • It looks like you want a North American week number, not an ISO week number.

    >>> a = datetime.datetime(2012, 1, 1)
    >>> a.strftime("%Y %U")
    '2012 01'