title: How to calculate difference between two dates in weeks in python
body: <p>I'm trying to calculate the difference between two dates in "weeks of year". I can get the datetime object and get the days etc but not week numbers. I can't, of course, subtract dates because weekends can't be ensured with that. </p>
  
  <p>I tried getting the week number using <code>d1.isocalendar()[1]</code> and subtracting <code>d2.isocalendar()[1]</code> but the issue is that <code>isocalendar()[1]</code> returns <code>December 31, 2012</code> as week 1 (which supposedly is correct) but that means my logic cannot span over this date. </p>
  
  <p>For reference, here's my complete code: </p>
  
  <pre><code>def week_no(self):
      ents = self.course.courselogentry_set.all().order_by('lecture_date')
      l_no = 1
      for e in ents:
          if l_no == 1: 
               starting_week_of_year = e.lecture_date.isocalendar()[1] # get week of year
               initial_year = e.lecture_date.year   
          if e == self: 
              this_year = e.lecture_date.year
              offset_week = (this_year - initial_year) * 52
              w_no = e.lecture_date.isocalendar()[1] - starting_week_of_year + 1 + offset_week
              break 
          l_no += 1
      return w_no  
  </code></pre>
  
  <p>With this code, the lecture on Dec 31, 2012 ends up being -35. </p>