pydata

Keep Looking, Don't Settle

2019-06-01 Week 22 Linux bash date time

Shares From Internet

  1. Linux bash date time
export day=2019-06-02
#day=$(date +%Y-%m-%d)
sunday_1=$(date -d "$day -$(date -d $day +%w) days" +%Y-%m-%d)  # last sunday include current date
sunday_2=$(date -d "$day -$(date -d $day +%u) days" +%Y-%m-%d)  # last sunday not include current date

echo $sunday_1      #2019-06-02    
echo $sunday_2      #2019-05-26

last_sat=$(date -d "last saturday" +%Y-%m-%d)       # Last Saturday
echo "last saturday is " $last_sat                  # last saturday is  2019-06-01
echo $(date -d "$last_sat +1 day" +%Y-%m-%d)        # 2019-06-02

# The Sunday before last Saturday
sun_before_last_sat=$(date -d "$last_sat -$(date -d $last_sat +%w) days" +%Y-%m-%d)
echo $sun_before_last_sat       # 2019-05-26

echo '-----------------------'

# Loop through from the Sunday before last Saturday to last Saturday
d=$sun_before_last_sat
while [ $d != $(date -d "$last_sat +1 day" +%Y-%m-%d) ];do
 echo $d
 d=$(date -I -d "$d + 1 day")
done

2019-05-26
2019-05-27
2019-05-28
2019-05-29
2019-05-30
2019-05-31
2019-06-01

Reference

  1. Bash: Looping through dates
  2. Getting Yesterdays or Tomorrows Day With Bash Shell Date Command
  3. linux + add X days to date and get new virtual date