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 saturdy. Suppose
on saturday i made this file
weekly_user1_Jul-13-2013.sql
I ned to get that name in script which i run daily. so for script running on saturday i need to get todays date , buif its on sunday then i need to get last saturday date.
how can i do that
Use the now() method to get the current date and time. Or, If you have datetime in a string format, refer to converting a string into a datetime object. The weekday() method returns the day of the week as an integer, where Monday is 0 and Sunday is 6. For example, the date(2022, 05, 02) is a Monday.
To format date in DD-MM-YYYY format, use the command date +%d-%m-%Y or printf "%(%d-%m-%Y)T\n" $EPOCHSECONDS .
I used below code to find out yesterday and last friday 's date. It was working in server1. YESTERDAY=$(date --date="yesterday" +"%m%d%Y") echo YESTERDAY: $YESTERDAY; LASTFRIDAY=$(date --date='last Friday' +"%m%d%Y") echo LAST FRIDAY: $LASTFRIDAY; I need to move the script to Server2 where it gives below error.
$ date +"%b-%d-%Y" -d "last saturday"
Jul-13-2013
In python script:
from datetime import date
from datetime import timedelta
today = date.today()
last_saturday = today - timedelta(days= (today.weekday() - 5) % 7)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With