How to calculate difference between two dates in weeks in python

前端 未结 6 1522
花落未央
花落未央 2020-12-02 23:02

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,

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-02 23:23

    You may want to refer the Python CookBook (2005 edition) Recipe 3.3. The following code snippet is from the cookbook, does what you require.

    from dateutil import rrule
    import datetime
    def weeks_between(start_date, end_date):
        weeks = rrule.rrule(rrule.WEEKLY, dtstart=start_date, until=end_date)
        return weeks.count()
    

提交回复
热议问题