How to find number of whole weeks between dates?

Marko Rauhamaa marko at pacujo.net
Wed Jun 10 13:38:59 EDT 2015


Sebastian M Cheung <minscheung at googlemail.com>:

> Say in 2014 April to May whole weeks would be 7th, 14th 28th April and
> May would be 5th, 12th and 19th. So expecting 7 whole weeks in total

This program gives you the number of days between two dates given in the
YYYY-MM-DD format:

========================================================================
#!/usr/bin/env python3

import sys

def gregorian_day_count(isodate):
    year, month, day = map(int, isodate.split('-'))
    a, b = divmod(12 * year + month - 3, 12)
    return (a * 365 + (a >> 2) - (a * 1311 >> 17) + (a * 1311 >> 19) +
            + (31306 * b + 722 >> 10))

def main():
    print(gregorian_day_count(sys.argv[2]) - gregorian_day_count(sys.argv[1]))

if __name__ == '__main__':
    main()
========================================================================

Divide the number by 7 and you have your answer.


Marko



More information about the Python-list mailing list