The Best Way to Compare Dates

Hi to all.

When you have to compare a lot of dates in a SH code, there is a way to directly compare? For example, how can I check if two dates differ in less than a week?

Thank's for reading.

... by checking the Date FAQ.
Thanks for posting!

Hi vgersh99.

It's very usefull the post that you have linked me, but if I have asked for this question is becasuse I hoped that Unix be able to compare the dates directly.

Any way, I let here my own, scrip, solution.

#Gets the number of day of year by a date passed like an argument. 
#$1 is the day of the date, $2 the month and $3 the year.
function getDayOfYead() {

    dayOfYear=$1
    
    #Sum the month days.
    case $2 in
         1) dayOfYear=`expr $dayOfYear + 0`
            ;;
         2) dayOfYear=`expr $dayOfYear + 31`
            ;;
         3) dayOfYear=`expr $dayOfYear + 59`
            ;;
         4) dayOfYear=`expr $dayOfYear + 90`
            ;;
         5) dayOfYear=`expr $dayOfYear + 120`
            ;;
         6) dayOfYear=`expr $dayOfYear + 151`
            ;;
         7) dayOfYear=`expr $dayOfYear + 181`
            ;;
         8) dayOfYear=`expr $dayOfYear + 212`
            ;;
         9) dayOfYear=`expr $dayOfYear + 243`
            ;;
        10) dayOfYear=`expr $dayOfYear + 273`
            ;;
        11) dayOfYear=`expr $dayOfYear + 304`
            ;;
        12) dayOfYear=`expr $dayOfYear + 334`
            ;;
    esac

    #To the leap years.
    leapYear=`echo $[$3%4]`
    if [ $leapYear -eq 0 ]
    then
        leapYear=`echo $[$3%100]`
        if [ $leapYear -ne 0 ]
        then
            leapYear=`echo $[$3%400]`
            if [ $leapYear -eq 0 ]
            then
                dayOfYear=`expr $dayOfYear + 1`
            fi
        fi
    fi

    echo "$dayOfYear"
}

I use the adove code cause using this script and

date +%j

, is very easy to compare to dates.

Thanks for reading.