Date Compare tool


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date Compare tool
# 8  
Old 02-13-2018
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
Code:
#!/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.

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

[/code]

Last edited by jim mcnamara; 02-13-2018 at 07:53 PM..
# 9  
Old 02-22-2018
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?

Code:
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

# 10  
Old 02-22-2018
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.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare Date to today's date in shell script

Hi Community! Following on from this code in another thread: #!/bin/bash file_string=`/bin/cat date.txt | /usr/bin/awk '{print $5,$4,$7,$6,$8}'` file_date=`/bin/date -d "$file_string"` file_epoch=`/bin/date -d "$file_string" +%s` now_epoch=`/bin/date +%s` if then #let... (2 Replies)
Discussion started by: Greenage
2 Replies

2. UNIX for Beginners Questions & Answers

Compare 2 files with different keywords : use server health-check tool

I have two files to be compared to get the output of the differences. File1 has a lot more lists than File2. After searching a lot on this thread I'am unable to find the exact code that im willing to get. This will be used as 'pre-check'/post-check utility (health check Tool) to compare... (1 Reply)
Discussion started by: GeekyJimmy
1 Replies

3. UNIX for Beginners Questions & Answers

Compare date in .txt with system date and remove if it's lesser than system date

Can someone help me with the code wherein there is a file f1.txt with different column and 34 column have expiry date and I need to get that and compare with system date and if expiry date is <system date remove those rows and other rows should be moved to new file f2.txt . I don't want to delete... (2 Replies)
Discussion started by: Stuti
2 Replies

4. Shell Programming and Scripting

Compare the system date with date from a text file

I get the date that's inside a text file and assigned it to a variable. When I grep the date from the file, I get this, Not After : Jul 28 14:09:57 2017 GMT So I only crop out the date, with this command echo $dateFile | cut -d ':' -f 2,4The result would be Jul 28 14:57 2017 GMT How do I... (3 Replies)
Discussion started by: Loc
3 Replies

5. Shell Programming and Scripting

Shell script to compare two files of todays date and yesterday's date

hi all, How to compare two files whether they are same are not...? like i had my input files as 20141201_file.txt and 20141130_file2.txt how to compare the above files based on date .. like todays file and yesterdays file...? (4 Replies)
Discussion started by: hemanthsaikumar
4 Replies

6. Shell Programming and Scripting

Tool to compare two Linux machines

Hi ALL, I was looking out for any freeware tool which can compare config properties of 2 linux machines for ex java properties. Tried in Google but no luck. Any help would be greatly appreciated :) (3 Replies)
Discussion started by: nikhil jain
3 Replies

7. UNIX for Dummies Questions & Answers

using date tool in bash

date --date='10:30am + 1 hour' +%H:%M 11:30 produces date --date='10:30pm + 1 hour' +%H:%M produces 23:30 I want to do the following: TIME="1:30pm" date --date='$TIME + 1 hour' + %H:%M to produce 14:30 (1 Reply)
Discussion started by: efittery
1 Replies

8. Shell Programming and Scripting

Adding days to system date then compare to a date

Hi! I am trying to read a file and every line has a specific date as one of its fields. I want to take that date and compare it to the date today plus 6 days. while read line do date=substr($line, $datepos, 8) #date is expected to be YYYYMMDD if ; then ...proceed commands ... (1 Reply)
Discussion started by: kokoro
1 Replies

9. Shell Programming and Scripting

ksh compare dates INSIDE a file (ie date A is > date B)

In KSH, I am pasting 2 almost identical files together and each one has a date and time on each line. I need to determine if the first instance of the date/time is greater than the 2nd instance of the date/time. If the first instance is greater, I just need to echo that line. I thought I would... (4 Replies)
Discussion started by: right_coaster
4 Replies

10. Shell Programming and Scripting

Compare date from db2 table to yesterday's Unix system date

I am currently running the following Korn shell script which works fine: #!/usr/bin/ksh count=`db2 -x "select count(*) from schema.tablename"` echo "count" I would like to add a "where" clause to the 2nd line that would allow me to get a record count of all the records from schema.tablename... (9 Replies)
Discussion started by: sasaliasim
9 Replies
Login or Register to Ask a Question