nullonprog
nullonprog

Reputation: 119

Calculate number of days between 2 dates Unix Shell

I want to calculate number of days between two dates in the unix shell . I tried to do a minus calculation but it dosen’t work . This is my script

VAR1=$1
VAR2=$2
v_date_deb=`echo ${VAR1#*=}`
v_date_fin=`echo ${VAR2#*=}`

dif = ($v_date_deb - $v_date_fin)
echo dif

if [ "$v_date_deb" = "" ]
then
  echo "Il faut saisir la date debut.."
  exit
fi

if [ "$v_date_fin" = "" ]
then
  echo "Il faut saisir la date fin.."
  exit
fi

Upvotes: 1

Views: 562

Answers (1)

chrslg
chrslg

Reputation: 13511

One attempt (but shot in the dark, since we don't know what is in your VAR1 variables)

ts1=$(date -d "${VAR1#*=}" +"%s")
ts2=$(date -d "${VAR2#*=}" +"%s")
dt=$(( (ts2 - ts1) / 86400 ))

Note the remark from William Pursell above: this solution is dependent on your "date" version. Date is not a built-in command from bash. And, particularly, the -d option (that allows to use the date specified instead of the current date that date is supposed to use otherwise, when used in "print the date" mode) is not common to all "date".

Upvotes: 1

Related Questions