1836770 Members
2259 Online
110109 Solutions
New Discussion

Re: compare 2 dates ..

 
SOLVED
Go to solution
someone_4
Honored Contributor

compare 2 dates ..

Hey everyone
how would i go about comparing 2 dates for example:
DATE1=7-16-2001
DATE2=7-12-2001
if [$DATE1 > $DATE2]
then
another command
else
this command
fi

I can get it to work with numbers but not dates.
Thanks

Richard
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor

Re: compare 2 dates ..

Hi Richard:

In the example, with the format you cited, do:

if [ $DATE1 -gt $DATE2 ]

...but it's better to test date relationships by constructing the date as CCYYMMDD. So, in this case, I'd use 'cut' or 'awk' to rearrange the string before testing it.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: compare 2 dates ..

Hi Richard,

I have the perfect answer for this. The way to do this is convert a MM DD YYYY into a Julian Day (~ days since 01/01/4712 BCE - used by astronomers to make calculations easy).

Use it like this:
DAY1=`caljd.sh 07/16/2001`
DAY2=`caljd.sh 07/19/2001`

You can then do numerical operations on the two
values. The routine when supplied with 1 arg will assume that it is a Julian Day and convert it to a calander day. It's also very handy for taking a date and find out what the date is 37 days from then. You can also use it to determine the day of the week. caljd.sh -u will give full usage.

Enjoy, Clay
If it ain't broke, I can fix that.
Mark Stewart
Frequent Advisor

Re: compare 2 dates ..

you could just break the date down into 3 separate variables for each date (6 total) and compare each using nested if statements. Kinda long, but it would definately work.
Abel Berger
Regular Advisor

Re: compare 2 dates ..

Hi Richard,

This is a example for you.
This is script find date a given number of days ago :
===========================================
set printnums = 0

# Check for -n switch. Print day & month in numbers rather than word (i.e. 0-6 and 1-12)
if ("$1" == "-n") then
set printnums = 1
shift
endif
if ("$2" == "-n") set printnums = 1

@ offset = `echo $1 | awk '{printf("%d",$0)}'`

if ($offset < 1) then
echo "Usage: `basename $0` [-n] "
exit 1
endif

if ($offset > 730000) then
echo "Get real. This is before the birth of Christ."
exit 1
endif


@ year = `date +'%Y'`
@ newyear = $year
set dayname = `date +'%a'`
set newdayname = $dayname
@ monthnum = `date +'%m'`
@ newmonthnum = $monthnum
set month = `date +'%b'`
set newmonth = $month
@ datenum = `date +'%e'`
@ newdatenum = $datenum
@ daynum = `date +'%u'`
@ newdaynum = $daynum

if (($datenum - $offset) > 0) then # If same month
@ newdatenum = ($datenum - $offset)
else # Otherwise ...
@ newdatenum = ($datenum - $offset) # newdatenum is now negative
while ($newdatenum < 1)
@ monthnum--
if ($monthnum < 1) then
@ monthnum = 12
@ newyear--
endif
set newmonth = `echo $monthnum | awk 'BEGIN{month[1]="Jan";month[2]="Feb";month[3]="Mar";month[4]="Apr";month[5]="May"; month[6]="Jun";month[7]="Jul";month[8]= "Aug";month[9]="Sep";month[10]="Oct"; month[11]="Nov";month[12]="Dec"}{print month[$0]}'`
switch ($newmonth)
case Apr:
case Jun:
case Sep:
case Nov:
@ newdatenum += 30
breaksw
case Feb:
if (($newyear % 4) == 0) then
@ newdatenum += 29 # Leap year
else
@ newdatenum += 28 # Non leap year
endif
breaksw
default:
@ newdatenum += 31
endsw
end
endif

# Calc dayname
@ tmp = ($offset % 7)
if (($daynum - $tmp) >= 0) then
@ daynum -= $tmp
else
@ daynum += (7 - $tmp)
endif
set newdayname = `echo $daynum | awk 'BEGIN{day[0]="Sun";day[1]="Mon";day[2]="Tue";day[3]="Wed";day[4]="Thu"; day[5]="Fri";day[6]="Sat";day[7]="Sun"}{print day[$0]}'`

# Print results
if ($printnums == 1) then
printf "%.1d %.2d %.2d %.4d\n" $daynum $monthnum $newdatenum $newyear
else
printf "%3s %3s %2d %4d\n" $newdayname $newmonth $newdatenum $newyear
endif

exit 0
==============================================

I think this help.

Regards,

Abel Berger


Sachin Patel
Honored Contributor

Re: compare 2 dates ..

I have this script where I check/compare for dates it is in perl. I am just copying whole script. I am not good programmer. but it works fine for me.

#!/usr/local/bin/perl
chomp @listsystems = ( list of systems);
%User = ();

open(FILE,">who.list")|| die "who.list cannot be opened\n";

chomp($Curr_Month = `/usr/bin/date +%b`);
chomp($Curr_Date = `/usr/bin/date +%d`);
chomp($Curr_Weekday = `/usr/bin/date +%u`);

print FILE "#$system\n";
@whoinfo = `remsh $system -n who -u |grep old |sort`;
foreach (@whoinfo){
if (/console/) {
next;
}
($uname,$t2,$Month,$Date,$t3,$t4,$pid,$t6) = split(/\s+/,$_);
$result=`remsh $system -n ps -ef |grep $pid|grep $uname`;
# print "$uname $pid $system\n";
# print "$result\n";
# $wait=;
if ($result eq ""){ #checking for valid login
next;
}
if ($Month ne $Curr_Month){
if ($Curr_Date > 10){
$User{$uname}++;
print FILE "$_";
next;
}
if ($Curr_Date < 10 && $Date < 25){
$User{$uname}++;
print FILE "$_";
next;
}
}
if ($Curr_Date > 15 && $Date < 5){
#print "2";
chomp;
$User{$uname}++;
print FILE "$_ more then 10 Days old\n";
next;
}
if ($Curr_Date > 20 && $Date < 10){
chomp;
$User{$uname}++;
print FILE "$_ more then 10 Days old\n";
next;
}

}#whoinfo
}#listsystems
close(FILE);
foreach $user (sort keys %User){
open(FILE,"who.list")||die "Can't open who.list file\n";
open(TEMP,">user.old")|| die "Can't open user.old file\n";
$sum=0;
print TEMP "Following are your old logins check them and kill them if necessary\n";
print TEMP "using listed PID \n";
print TEMP "If you are not able to logout or kill them contact support\n\n";
print TEMP "NAME LINE TIME IDLE PID COMMENTS\n";
$found=0;

while(){
($uname,$t2) = split(/\s+/,$_);
if (/^#/) {
$sysname=$_;
$sysname_print=1; #print sys name if it is new system
}
if ($user eq $uname){
if ($sysname_print == 1){ # print system name and
print TEMP "\n$sysname";
$sysname_print=0; # make variable =0 so next time it will not print sysname
}
$found=1;
print TEMP "$_";
$sum++;
}
} #while

print TEMP "\n\nThanks for your help\n\n\n";
close(TEMP);
close(FILE);
# $user1="sachin";
if ($found == 1){ # send mail only if found any old login
# print "send to $user";
system("/usr/bin/cat /home/sachin/myscript/cronjobs/user.old");
system("/usr/bin/cat /home/sachin/myscript/cronjobs/user.old | /bin/mailx -s 'OLD LOGIN' $user");
}
#$wait=;
}#foreach


Is photography a hobby or another way to spend $
James R. Ferguson
Acclaimed Contributor

Re: compare 2 dates ..

Hi (again) Richard:

Oops! I missed that fact that you were comparing stings, not integers. You could strip the "-" out and compare as two numbers or extract and rearrange before comparing:

# X=`echo $DATE1|tr -d "-"`
# Y=`echo $DATE2|tr -d "-"`

...or:

# X=`echo $DATE1|awk -F- '{print $3 $1 $2}'`
# Y=`echo $DATE2|awk -F- '{print $3 $1 $2}'`

...and then test as integers:

if [ $X -gr $Y ]

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: compare 2 dates ..

Hi again Richard,

I didn't notice a small mistake in my posting that I'm sure you've caught; it should be:
DAY1=`caljd.sh 07 16 2001` rather that 07/16/2001

It needs the three args to know this a calendar date and I want a Julian Day rather than the reverse.

Clay
If it ain't broke, I can fix that.