difference in date

Hi All!

I would like to know the time difference between two dates which are in same format...

$ date -r abc
Thu Oct 29 09:40:37 EDT 2009
$ date
Fri Oct 30 02:07:03 EDT 2009

i would like to find the diff between these two dates in hours..please help..:slight_smile:

Regards,
Kiran

Hello.

You can use:

ini=`date +'%s'`; sleep 5; fin=`date +'%s'`
difference=`expr $fin - $ini`
echo $difference
# Calc the difference between 2 dates in seconds, minutes, hours or days
dateDiff ()    {    # 1: [-s, -m, -h, -d]    2: Date1    3: Date2
    case $1 in
        -s )    SEC=1;    shift    ;;
        -m )    SEC=60;    shift    ;;
        -h )    SEC=3600;    shift    ;;
        -d )    SEC=86400;    shift    ;;
        * )    SEC=86400    ;;
    esac
    DTE1=$(date -u --date "$1" +%s) # Converts date into UNIX timestamp
    DTE2=$(date -u --date "$2" +%s)
    if [ $DTE1 -gt $DTE2 ]
    then    let "DIFFSEC=(DTE1-DTE2)/SEC"
    else    let "DIFFSEC=(DTE2-DTE1)/SEC"
    fi
    echo $DIFFSEC
}
echo $((($(date +%s) - $(date -r abc +%s))/3600))