Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compare 2 dates

Status
Not open for further replies.

jprabaker

Technical User
May 31, 2001
185
GB
I am trying to write a routine that will compare 2 dates in the format of dd/mm/yy. I want to test a series of dates against one I provide and perform an action if the tested date is older than the supplied date.

Any suggestions would be greatfully received.

Thanks,

John
 
Hi,

to be sure, I would count the days for the given date, as this is the smallest most correct unit in this format of date you can get and then compare the number of days with each other. Problem would be that you have to tell your script if it gets less than the year 2000 ie. 00. If you don't have any dates to be compared but from the year 2000+, I think that 1st thought will be enough. Else adds a "19" in front of the "yy" if it gets higher then... lets say 70 for "yy" :) Ok, in 2070 you will have to change this script, but I think that will be others people problem.

laters
zaxxon
 
What have you so far ?
For comparison purpose, you may consider to convert your date values from dd/mm/yy to yymmdd, like this:
ymdDate=`echo $dmyDate | awk -F/ '{printf "%02d%02d%02d",$3,$2,$1}'`

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Not sure what you mean, do you want to take one date away from the other or just do a straight forward text compare


a=`date +%d/%m/%y`

b="17/05/04"


if [ "$a" != "$b" ];then
do stuff
else
do other stuff
fi

Can you give a little more detail, also what O/S and shell?

--
| Mike Nixon
| Unix Admin
|
----------------------------
 
Wow, near instant replies! Thanks guys. I'm trying to list (on AIX) all of the software that was installed after a certain date. This would have been simple as I could have done a "grep -v date" but I get some software being listed with an earlier date, and want to look at these seperatly.

I think I should be able to just count the days.


Thanks,

John
 
John,


One other thought although it wasn't what you asked would be to consider using the -newer option of the find command.

This option can also be used with a ! -newer to get files that are older than the baseline file you are using for the comparison.

It's output could always be redirected to temp file that you could delete once you validated what you were searching for.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top