Find the Friday of previous/last week in python

后端 未结 5 1710
闹比i
闹比i 2020-12-25 08:01

Eg1. Suppose I have a day 4/30/07 .Then I need to get 4/27/07.

Eg2. Suppose I have a day 6/29/07 .Then I need to get 6/22/07.

5条回答
  •  一生所求
    2020-12-25 08:26

    An another and easier way is to use python-dateutil. To get the previous Friday :

    >>> from dateutil.relativedelta import relativedelta, FR
    >>> from datetime import datetime
    >>> datetime(2015, 7, 8) + relativedelta(weekday=FR(-1))
    datetime.datetime(2015, 7, 3, 0, 0)
    

    And the next Friday :

    >>> datetime(2015, 7, 8) + relativedelta(weekday=FR(+1))
    datetime.datetime(2015, 7, 10, 0, 0)
    

提交回复
热议问题