1
 datetime.date(2010, 1, 1).isocalendar()[1]

gives week number 53 since 1st Jan 2010 was in a week which started in 2009. However, I would like to start Jan 1, 2010 as week 1. Is there any option in datetime isocalendar to do this?

If I follow the solution at How can I get the current week using Python?, I get 53 as result

Community
  • 1
  • 1
user308827
  • 21,227
  • 87
  • 254
  • 417

2 Answers2

4

Compute the number of days since the first of the year, integer-divide by 7 and add 1:

>>> import datetime as DT
>>> (DT.date(2010,1,1)-DT.date(2010,1,1)).days // 7 + 1
1

This notion of week number is very different from the ISO week definition, so you aren't going to find an option to do this in isocalendar.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
0

If you're going to be getting data in total weeks since a point, you can mod that number by the number of weeks in a year (52) to get the remainder (ie, how many weeks into the year you are):

>>> 53 % 52
>>> 1
>>> 925 % 52
>>> 41
a p
  • 3,098
  • 2
  • 24
  • 46