As previous answers, you can use the -d
switch like :
date -d "$myDate + 10 days"
But you need to be careful with the time zone!
An example of something that can go wrong :
If you want to get an interval for a day like [start of day, end of day] for the "8 march 2020" with the timezone at Montreal, where the DST start at 2:00:00 am, you could do
startDate=$(date --iso-8601=n -d "8 march 2020 00:00:00.000000001")
endDate=$(date --iso-8601=n -d "$startDate + 1 day")
but instead of
startDate → "2020-03-08T00:00:00,000000001-0500"
endDate → "2020-03-09T00:00:00,000000001-0400"
you'll get 1 hour of the 9 march 2020
startDate → "2020-03-08T00:00:00,000000001-0500"
endDate → "2020-03-09T01:00:00,000000001-0400"
To fix that, you could do the following :
# Returns the difference between two dates' local time offsets. E.g. : "3600" or "- 1800"
getLocalTimeOffsetDiffInSeconds() {
# Get the offset in the following format: -0530 for -05h30min
localOffset1=$(date -d "$1" +%z)
localOffset2=$(date -d "$2" +%z)
# Get the offset in seconds
localOffsetInSec1=$(echo $localOffset1 | sed -E 's/^([+-])(..)(..)/scale=2;0\1(\2 * 3600 + \3 * 60)/' | bc)
localOffsetInSec2=$(echo $localOffset2 | sed -E 's/^([+-])(..)(..)/scale=2;0\1(\2 * 3600 + \3 * 60)/' | bc)
# Compute diff
diffOffsetInSec=$(echo "$localOffsetInSec2 - $localOffsetInSec1" | bc)
# Add a space between the sign and the value, so it can be used by the command "date".
echo "$diffOffsetInSec" | sed -E 's/([+-])(.*)/\1 \2/'
}
startDate=$(date --iso-8601=n -d "8 march 2020 00:00:00.000000001")
endDate=$(date --iso-8601=n -d "$startDate + 1 day")
# Fixes endDate if the DST changed between the two dates.
dstDiff=$(getLocalTimeOffsetDiffInSeconds "$startDate" "$endDate")
# dateFix will be a string to remove the dstDiff from the date in a format
# that can be understood by the command "date". E.g. "3600 seconds" or "- 3600 seconds".
dateFix=$(echo "$(echo "- $dstDiff" | bc | sed -E 's/([+-])(.*)/\1 \2/')")
endDate=$(date --iso-8601=n -d "$endDate $dateFix seconds")