Date Difference function in unix

Post date: Nov 12, 2014 12:19:8 AM

[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