Issue
I have two variables date1=2023-10-08
and date2=2023-10-09
I want to calculate the difference between the two in days , I'm trying with:
DIFF=$(( ($date2 - $date1) / (60 * 60 * 24) ))
if [ $DIFF -eq 1 ]
then
echo "one day ago.."
else
echo "many dayzz"
fi
I'm getting this error:
(2023-10-09: value too great for base (error token is "09")
Solution
Bash arithmetic cannot process date within this format. Dates must be converted to integers with a tool such as date
. Using GNU date
's %s
, one can compute the difference with
$(( ($(date -d "$date2" "+%s") - $(date -d "$date1" "+%s")) / 86400 ))
Answered By - Loïc Reynier Answer Checked By - Timothy Miller (WPSolving Admin)