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

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

    This will give the first next Monday after given date:

    import datetime
    
    def get_next_monday(year, month, day):
        date0 = datetime.date(year, month, day)
        next_monday = date0 + datetime.timedelta(7 - date0.weekday() or 7)
        return next_monday
    
    print get_next_monday(2011, 7, 2)
    print get_next_monday(2015, 8, 31)
    print get_next_monday(2015, 9, 1)
    

    2011-07-04
    2015-09-07
    2015-09-07

提交回复
热议问题