Thursday, April 23, 2020

datediff bash function

Datediff function to return the number of days between two dates.  It's not too robust, but I got it working on MacOS and Linux, mostly to accommodate the differences between GNU Util date and HomeBrew BSD date...

datediff() {
    date --version >> /dev/null 2>&1
    RETVAL=$?
    if [ -e /usr/local/bin/gdate ] && [ $RETVAL -ne 0 ] ;
    then
        DATEBIN=/usr/local/bin/gdate
    else
        DATEBIN=`which date`
    fi

    d1=$($DATEBIN -d "$1" +%s)
    d2=$($DATEBIN -d "$2" +%s)
    echo $(( (d1 - d2) / 86400 )) days
}

No comments:

Post a Comment