Weeks difference between two dates in python
<p>How do I get the differences between two valid dates in weeks. I have googled many, but none are the one that I have been looking for</p>

<p>Say I have two dates:</p>

<blockquote>
  <p><strong>02-Dec-2016</strong> and <strong>10-Jan-2017</strong>.</p>
</blockquote>

<p>I want it to provide me with output like following</p>

<pre><code>02-Dec-2016 - 04-Dec-2016 (2 days) (2 days before monday comes)
05-Dec-2016 - 08-Jan-2017 (5 weeks) (starts from monday-sunday)
08-Jan-2017 - 10-Jan-2017 (2 days) (2 days after monday has gone)
</code></pre>

<p>This is what you actualy want:    </p>

<pre><code>import datetime


def diff(d1, d2):
    result = []
    delta = datetime.timedelta(days=0)
    day = datetime.timedelta(days=1)

    while d1.weekday() != 0:
        d1 += day
        delta += day
    result.append((d1 - delta, d1 - day))

    weeks, days = divmod((d2 - d1).days, 7)
    d3 = d1 + datetime.timedelta(weeks=weeks)
    d4 = d3 + datetime.timedelta(days=days)

    result.append((d1, d3 - day))
    result.append((d3, d4))
    return result


d1 = datetime.date(2016, 12, 2)
d2 = datetime.date(2017, 01, 10)

for i,j in diff(d1,d2):
    print '{} - {} ({} days)'.format(datetime.datetime.strftime(i, "%d-%b-%Y"), datetime.datetime.strftime(j, "%d-%b-%Y"), (j-i).days + 1)

# 02-Dec-2016 - 04-Dec-2016 (3 days)
# 05-Dec-2016 - 08-Jan-2017 (35 days)
# 09-Jan-2017 - 10-Jan-2017 (2 days)
</code></pre>

python