Find Monday's date with Python

前端 未结 8 2031
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 03:22

How do I find the previous Monday\'s date, based off of the current date using Python? I thought maybe I could use: datetime.weekday() to do it, but I am gettin

8条回答
  •  星月不相逢
    2020-12-13 03:52

    I think the easiest way is using python-dateutil like this:

    from datetime import date
    from dateutil.relativedelta import relativedelta, MO
    
    today = date.today()
    last_monday = today + relativedelta(weekday=MO(-1))
    print last_monday
    

提交回复
热议问题