Days difference between two dates

Hello,

I would like to find out the number of days between two dates of the format yyyy-mm-dd.

Any help on this is highly appreciated.

Thanks.

try this to start - note you have to handle cross-years eg 2007 -> 2008

#!/bin/ksh
yday()
{
        perl -e '
                use POSIX qw(strftime);
                $fmt = "%j";  # %j day of year 1 - 366
                $mday = substr("$ARGV[0]", 8, 2);
                $mon =  substr("$ARGV[0]", 5 ,2);
                $year = substr("$ARGV[0]", 0 ,4);                  
                $weekday =
                  strftime($fmt, 0, 0, 0, $mday , $mon - 1, $year - 1900, -1, -1, -1);
                print int $weekday;
                ' "$1"
}

today=$( yday 2008-03-10 )
oldat=$( yday 2008-01-03 )
ddiff=$(( $today - $oldat ))
echo "$ddiff"


thanks a lot. this is really so helpfull to me...........i will never forget your help in this

A handy one :slight_smile:

$ cat daysdiff.sh
D1=`date +%s -d "2007-12-19"`
D2=`date +%s -d "2008-02-09"`
((diff_sec=D2-D1))
echo - | awk -v SECS=$diff_sec '{printf "Number of days : %d",SECS/(60*60*24)}'

//Jadu

1 Like

Thanks a lot for the replies.

But, I use HP-UX and it does not support date %s. Hence I need a different way to find the difference between the dates in a shell script.

Do you have Perl? [innocent glance]

No I do not have Perl. This needs to be strictly in ksh.