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,018
  • 70
  • 229
  • 377

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
  • 777,569
  • 165
  • 1,697
  • 1,613
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
  • 2,922
  • 2
  • 24
  • 45