Date Difference function in unix

posted Nov 11, 2014, 4:19 PM by Sachchida Ojha
[gpadmin@sachi scripts]$ datediff (){
case $1 in
 -s)   sec=1;      shift;;
 -m)   sec=60;     shift;;
 -h)   sec=3600;   shift;;
 -d)   sec=86400;  shift;;
  *)    sec=86400;;
 esac
 dte1=$(date2stamp $1)
 dte2=$(date2stamp $2)
diffSec=$((dte2-dte1))
 if ((diffSec < 0)); then abs=-1; else abs=1; fi
echo $((diffSec/sec*abs))
}

[gpadmin@sachi scripts]$ datediff -s "2006-10-01" "2006-10-02"
86400
[gpadmin@sachi scripts]$ datediff -m "2006-10-01" "2006-10-02"
1440
[gpadmin@sachi scripts]$ datedDiff -h "2006-10-01" "2006-10-02"
24
[gpadmin@sachi scripts]$ datediff -d "2006-10-01" "2006-10-02"
1


Comments