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

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

    via list comprehension?

    from datetime import *
    [datetime.today()+timedelta(days=x) for x in range(0,7) if (datetime.today()+timedelta(days=x)).weekday() % 7 == 0]
    
    (0 at the end is for next monday, returns current date when run on monday)
    

提交回复
热议问题