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

后端 未结 11 1659
被撕碎了的回忆
被撕碎了的回忆 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:34

    Try

    >>> dt = datetime(2011, 7, 2)
    >>> dt + timedelta(days=(7 - dt.weekday()))
    datetime.datetime(2011, 7, 4, 0, 0)
    

    using, that the next monday is 7 days after the a monday, 6 days after a tuesday, and so on, and also using, that Python's datetime type reports monday as 0, ..., sunday as 6.

提交回复
热议问题