Find the date for the first Monday after a given a date

后端 未结 11 1695
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 15:47

Given a particular date, say 2011-07-02, how can I find the date of the next Monday (or any weekday day for that matter) after that date?

11条回答
  •  忘掉有多难
    2020-11-27 16:35

    You can start adding one day to date object and stop when it's monday.

    >>> d = datetime.date(2011, 7, 2)
    >>> while d.weekday() != 0: #0 for monday
    ...     d += datetime.timedelta(days=1)
    ... 
    >>> d
    datetime.date(2011, 7, 4)
    

提交回复
热议问题