How to find out the date of the last Saturday in Linux shell script or python?

后端 未结 3 1859
臣服心动
臣服心动 2021-01-02 23:38

I have python script which i need to run daily for backups. Now i need to find the date of last saturday because i need that in my script to get the backups i did on last sa

相关标签:
3条回答
  • 2021-01-03 00:11

    In python script:

    from datetime import date
    from datetime import timedelta
    today = date.today()
    last_saturday = today - timedelta(days= (today.weekday() - 5) % 7)
    
    0 讨论(0)
  • 2021-01-03 00:20

    On Mac OS it would be:

    $ date -v -sat +"%b-%d-%Y"
    Mar-28-2015
    
    0 讨论(0)
  • 2021-01-03 00:23
    $ date +"%b-%d-%Y" -d "last saturday"
    Jul-13-2013
    
    0 讨论(0)
提交回复
热议问题