Bash script compare two date variables [duplicate]

The GNU date command can convert a date into the number of seconds since 1970. Try this script:

#! /bin/bash
DATE=$(date -d "$3-$2-$1 01" '+%s')
COUNT=0
tr "https://stackoverflow.com/" ' ' | {
    while read D M Y ; do
    THIS=$(date -d "$Y-$M-$D 01" '+%s')
    if (( THIS > DATE )) ; then
        COUNT=$((COUNT + 1))
    fi
    done
    echo $COUNT
}

It expects three arguments and the raw dates in stdin:

for D in $(seq 19 25) ; do echo $D/08/2007 ; done | ./count.sh 22 08 2007
3

It will work till 2038. 😉

Leave a Comment