Date Compare tool

Hello Guys,
Just joined today with a simple task i guess.
I need to write a shell script which takes input from users which is a date and should be able to compare the date differences between the dates keeping in mind the leap year and all.

Kindly Guide :slight_smile: :smiley:

Kindly think about what you are asking. Then give us a few more details:

  1. What operating system are you using?
  2. What shell do you want to use to run your shell script?
  3. In what format will dates be entered?
  4. Where will the dates be found? (Are they provided as variables in your shell script? Are they command-line parameters? Are they typed in in response to prompts? Are they found in known files? ...)
  5. In what way do you want dates to be compared? (Are you just trying to determine if they specify the same date? Are you trying to determine the number of days between two dates? If your dates includes hours, minutes, and seconds; are you trying to determine the number of seconds between the two dates?)

Please help us help you.

1 Like

What operating system are you using?
---This is a Linux.
What shell do you want to use to run your shell script?
---Bash Shell
In what format will dates be entered?
---Date will be entered as YYYYMMDD whcih i have split into 3 variables.
Where will the dates be found? (Are they provided as variables in your shell script? Are they command-line parameters? Are they typed in in response to prompts? Are they found in known files? ...)
---Dates typed in response to prompts.
In what way do you want dates to be compared? (Are you just trying to determine if they specify the same date? Are you trying to determine the number of days between two dates? If your dates includes hours, minutes, and seconds; are you trying to determine the number of seconds between the two dates?).

---i Want to know the difference in Days and not using Datediff or Date. I just need to use simple if ...else and arithmetics. Also look for number of leap years

Is this a homework assignment? Homework and coursework questions can only be posted in the Homework & Coursework Questions forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

If you did not post homework, please explain the company you work for and the nature of the problem you are working on. And, explain why datediff and date cannot be used in your script!

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

I am doing this as a practice to learn looping Artithmetic and all
Could you debug this for me please . I am just calculating the year difference with leap year in this phase. I know i have messed up the Loops

#!/nairvigv/bin/bash
echo "ENTER FIRST DATE IN YYYYMMDD format"
read startdate



startyear=${startdate:0:4}
startmonth=${startdate:4:2}
startday=${startdate:6:2}




echo The date you Entered is $startyear"-"$startmonth"-"$startday


echo "ENTER SECOND DATE IN YYYYMMDD format"
read enddate
endyear=${enddate:0:4}
endmonth=${enddate:4:2}
endday=${enddate:6:2}



echo The date you Entered is $endyear"-"$endmonth"-"$endday



days_months=(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

echo ${days_months[*]}
##################### YEARS######################################################
if [ $startyear -eq $endyear ]

then
        yeardifference=0;
        echo $yeardifference

 else
        if [ $startyear % 4 == 0 && $startyear % 100 != 0 || $startyear % 400 == 0 ] then
                if[ $endyear % 4 == 0 && $endyear % 100 != 0 || $endyear % 400 == 0 ] then
                        if[ $startyear -gt $endyear ] then
                        yeardifference=`expr $startyear - $endyear * 365 + 2`
                        else
                        yeardifference=`expr $endyear - $startyear * 365 + 2`

                        fi
                        if[ $endmonth -gt $startmonth ]then
                                if( "${days_months[$startmonth - 1]}" -gt  "${days_months[1]}" )then
                                        yeardifference=yeardifference-1;
                                fi
                        fi
                fi
 else
                   if[ $startyear -gt $endyear ] then
                        yeardifference=`expr $startyear - $endyear * 365 + 1`
                        else
                        yeardifference=`expr $endyear - $startyear * 365 + 1`
                        
                        fi
                        if[ $startmonth -gt $endmonth ]then
                                if( "${days_months[$endmonth - 1]}" -gt  "${days_months[1]}" )then
                                        yeardifference=yeardifference-1;
                                fi
                        fi
                fi


else

                if[ $startyear -gt $endyear ]then
                         yeardifference=`expr $startyear - $endyear * 365`

                        else
                         yeardifference=`expr $endyear - $start * 365`
                fi



fi


Sure this DOES NOT belong to a class nor course?
What does "and all" mean - you use it twice ("leap year and all", "looping Artithmetic and all"), but it doesn't help understand your requirements.

You say "I know i have messed up the Loops", but I'm afraid it's more than the loops. WHAT is your expected result? It can't be derived from your code, as that is faulty, misleading, and overcomplicated, having syntax and logics errors. You assign a variable "yeardifference" in several lines which makes me believe that it should finally hold something like a result. But - once you have the expr syntax errors sorted out - you assign large negative values to it which makes me think there's a semantic error as well.

The are plenty of syntax errors, of which I at first glance found:

  • if[ $startyear -gt $endyear ] then has two sytax errors in it - it needs a space between if and [ , and a ; between ] and then , plus it occurs twice in different branches, the second in a dangling (belonging nowhere) else if I count correctly.
  • if with parentheses would work if a comand were given inside but would need a space as well.
  • yeardifference=yeardifference-1 will assign the unexpanded, unevaluated string yeardifference-1

I stopped here. Please get the syntaxy right. And, again - what exactly is the desired result?

In addition to what RudiC has already said, you say that you have messed up the loops; but there aren't any loops in your code! There are only nested if statements that seem to try to calculate the number of days in years without accounting for the number of days in months or the number of days between days within a month. You are correct in thinking that you need a couple of nested loops (one looping through months and one looping through years, and depending on how you structure your code, you might also want a loop to loop through days in a month), but your code does not contain any loops at all. Loops start with keywords like for , until , and while ; not with the keyword if .

One might note that the prompts given to your users asks for 10 character inputs in the format YYYY-MM-DD , but when you extract the year, month, and day fields from the entered strings, you only look at the first 8 of those 10 characters.

RudiC mentioned that you have a dangling else . I'm not sure it that is true or not. Your lack of consistent indentation makes it impossible to line up if s with their corresponding else s and fi s. But it is clear that you have more fi s than you have if s; and that has to be an error.

Instead of partially checking whether the start date comes before or after the end date so many times, you might want to check that just after you get the two dates from your user and switch their values if the end comes before the start.

Note also that if you have a date like December 31 in one year and January 1 in the next year, there is one day between them whether or not either of those years is a leap year. And, if you go from February 28 to March 1 that is going to be one day or two days depending on what year is being processed. Your code only calculates leap days if the years are different. And, if you go from March 1 in one leap year to February 28 in the next leap year; even though both years are leap years, there are no leap days in the four or eight years between those two dates.

And, finally, you might also note that you use a variable named start , but you never assign any value to it. I haven't verified that there aren't any other variables that are used without being set.

Start simple.

First off, straight comparisons of known correctly entered dates in the YYYYMMDD format can be treated as plain numbers for comparisons in shell using: -eq, -ne, -gt, -lt

#!/bin/bash

d1=20180113
d2=20170113
printf "Dates are d1=%d d2=%d\n" $d1 $d2
# different versions of if-then-else 
[ $d1 -gt $d2 ] && echo 'greater' || echo 'not greater'

if [ $d1 -eq $d2 ] ; then
  echo 'equal'
else
  echo 'not equal'
fi
$ ./dcomp.shl
Dates are d1=20180113 d2=20170113
greater
not equal

Next the units for dates are days. To do further math like subtraction, you have to work on the number of days the date represents. Time and date in UNIX is epoch seconds.
So doing Julian dates in shell is somewhat tedious but is about the only way to proceed, i.e., convert a date to some kind epoch days. Epoch means the number of days since an arbitrary start date. For UNIX this is number of seconds since the first second in January 1, 1970. There are 86400 seconds per day. You can ignore leap seconds usually.

Chris (CFA) Johnson has a script in bash to do this. It is not beginner fodder.
Go here: Chris F.A. Johnson in Toronto, Ontario, Canada See if you can get a copy of the bash recipes book listed on this now-inactive site. Chris still posts here on UNIX Forums every once in a while.

secs=$(date -d 20110101 +%s)  # epoch seconds for Jan 1 2011
days=$(( secs % 86400 ))
echo "epoch days=$days"

[/code]

hello Guys
Sorry for the delay was busy with other stuffs.
i have started from scratch.

So here i am trying to get the year difference if both year is leap year.

i am getting a syntax error. Can u help me?

echo "ENTER FIRST DATE IN YYYYMMDD format"
read startdate



startyear=${startdate:0:4}
startmonth=${startdate:4:2}
startday=${startdate:6:2}




echo The date you Entered is $startyear"-"$startmonth"-"$startday


echo "ENTER SECOND DATE IN YYYYMMDD format"
read enddate
endyear=${enddate:0:4}
endmonth=${enddate:4:2}
endday=${enddate:6:2}



echo The date you Entered is $endyear"-"$endmonth"-"$endday



days_months=(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

echo ${days_months[*]}
################################################ CALCULATE YEARS#######################################################################################
if [ $startyear -eq $endyear ]
then
yeardifference=0;
echo $yeardifference
else
         if[ $startyear % 4 == 0 && $startyear % 100 != 0 || $startyear % 400 == 0 ]then
         if[ $endyear % 4 == 0 && $endyear % 100 != 0 || $endyear % 400 == 0 ]then
                if [ $startyear -gt $endyear ]then
                yeardifference=$((($startyear - $endyear) * 365 + 2))
                echo $yeardifference
        else
                yeardifference=$((($endyear - $startyear) * 365 + 2))
                echo $yeardifference
                fi
        fi
        fi

fi

RudiC, jim mcnamara, and I all made made several comments that should have helped you. It looks like you used one of my suggested and ignored everything else.

If you aren't interested in reading our comments, applying our suggestions, and answering our questions, there isn't much that we can do to help you.

1 Like