Date arithmetic

Basic types / Date and time

#the time zone of the example; without it the local one is used
export TZ=UTC

#a fixed date keeps the output the same; for today drop the date
#and leave the words: date -d "yesterday", date -d "+1 month"
now="2024-02-28"

#GNU date -d understands days, months and years in words
yesterday=$(date -d "$now -1 day" +%F)
#yesterday is 2024-02-27

tomorrow=$(date -d "$now +1 day" +%F)
#tomorrow is 2024-02-29, a leap year

nextMonth=$(date -d "$now +1 month" +%F)
#nextMonth is 2024-03-28

nextYear=$(date -d "$now +1 year" +%F)
#nextYear is 2025-02-28

#trap: +1 month keeps the day, and 31 February moves on to March
fromJanuary31=$(date -d "2024-01-31 +1 month" +%F)
#fromJanuary31 is 2024-03-02

#the bash way: seconds since 1970 plus the days in seconds
start=$(date -d "$now 12:00" +%s)
printf -v in100Days '%(%F)T' "$((start + 100 * 24 * 60 * 60))"
#in100Days is 2024-06-07

echo "now is $now"
echo "yesterday is $yesterday"
echo "tomorrow is $tomorrow"
echo "nextMonth is $nextMonth"
echo "nextYear is $nextYear"
echo "fromJanuary31 is $fromJanuary31"
echo "in100Days is $in100Days"