0

I have a set of variables that have a date stored, e.g.

today=$(date '+%Y-%m-%d')
yesterday=$(date -d "yesterday 13:00" '+%Y-%m-%d')
day2=$(date -d "2 days ago 13:00" '+%Y-%m-%d')

they are stored in an array

DAYS=($day2 $yesterday $today)

now I want to loop over all the days in the array and for each day I want to have a temporary variable with the date on day before:

for i in "${DAYS[@]}"
do

j=should be date of i minus 1 day (1 day before date of i)

done

How do I subtract one day (or for that matter any other amount of time) from a date that's stored in a variable?

Thanks, Patrick

Patrick
  • 65
  • 5
  • You are looking for something like `prevday=$(date -d "$date -1 days" +"%Y-%m-%d")` where **$date** is the current date and the format is as you desire. Basically this is what you were doing to get the dates array, but the first argument of the function is the date from which you want to subtract. – codtex Nov 26 '20 at 08:43
  • So far I was unable to find where to input the variable name (in your case $date) into the calculation. This is exactly what I was looking for. Thanks. – Patrick Nov 26 '20 at 08:57

0 Answers0