Number of days between the current date and user defined date

I am trying to find out the number of days between the current date and user defined date.

I took reference from here for the date2jd() function.
Modified the function according to my requirement. But its not working properly.

Original code from here is working fine.

#!/bin/sh
########### date2jd function ############
date2jd() {
 integer ijd day month year mnjd jd lday
        year=$1
        month=$2
        day=$3
        echo $year
        echo $month
        echo $day
        ((standard_jd = day - 32075 
           + 1461 * (year + 4800 - (14 - month)/12)/4 
           + 367 * (month - 2 + (14 - month)/12*12)/12 
           - 3 * ((year + 4900 - (14 - month)/12)/100)/4))
        ((jd = standard_jd-2400001))
 echo $jd
        return 0
}
########### user defined date ###########
echo 'Enter date in the format dd/mm/yyyy'
read DATE
DD=`echo $DATE | cut -c1-2`
MM=`echo $DATE | cut -c4-5`
YYYY=`echo $DATE | cut -c7-10`
####### current date ############
DD2=`date '+%d'`
MM2=`date '+%m'`
YYYY2=`date '+%Y'`
jd1=$(date2jd $YYYY1 $MM1 $DD1) || exit $?
jd2=$(date2jd $YYYY2 $MM2 $DD2) || exit $?
((jd3=jd2-jd1))
  echo $jd3

Please help.

Some days ago about same question.

You have variables YYYY DD MM, but try to use DD1, YYYY1, MM1.
And in function you have more than one echo. If you need test echo in function, then redirect it ex. to the stderr.

echo $year >&2

Now you echo your all values to the jd1 or jd2.

Read input version 2

echo 'Enter date in the format: dd mm yyyy'
read DD MM YYYY XSTR

No need to parse and so on.

Or

echo 'Enter date in the format: dd/mm/yyyy'
IFS="/" read DD MM YYYY XSTR
echo "$DD - $MM - $YYYY"

Or ...

1 Like