How to increment a date in a Bash script How to increment a date in a Bash script bash bash

How to increment a date in a Bash script


Use the date command's ability to add days to existing dates.

The following:

DATE=2013-05-25for i in {0..8}do   NEXT_DATE=$(date +%m-%d-%Y -d "$DATE + $i day")   echo "$NEXT_DATE"done

produces:

05-25-201305-26-201305-27-201305-28-201305-29-201305-30-201305-31-201306-01-201306-02-2013

Note, this works well in your case but other date formats such as yyyymmdd may need to include "UTC" in the date string (e.g., date -ud "20130515 UTC + 1 day").


startdate=$(date -d"$1" +%s)next=86400 # 86400 is one dayfor (( i=startdate; i < startdate + 8*next; i+=next )); do     date -d"@$i" +%d-%m-%Ydone


Just another way to increment or decrement days from today that's a bit more compact:

$ date %y%m%d ## show the current date$ 20150109$ ## add a day:$ echo $(date %y%m%d -d "$(date) + 1 day")$ 20150110$ ## Subtract a day:$ echo $(date %y%m%d -d "$(date) - 1 day")$ 20150108$