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 dates

Status
Not open for further replies.

BobRaney

Programmer
Nov 22, 2001
59
US
Hello,

How can I compare dates in a script?

If 07/21/04 > 07/20/04
then echo "greater"
else echo "not greater"
fi

Thanks
Raney
 
you cannot compare MMDDYYYY,
but YYYYMMDD vs YYYYMMDD
loock around, there are a lot of threads playing
with split&&formating strings, computing date...

echo 07/20/04|sed -e 's/\(.*\)\/\(.*\)\/\(.*\)/\3\1\2/'
will transform 07/20/04 to 040720
and 040720 is now comparable to 040728
BUT this assume the input is exactly xx/xx/xx
the sed works, what if input is: 1/1/2004 vs 12/31/04
so:
- carefully check the input
- translate it to comparable strings
- compare the values
 
For more advanced date manipulation, check out this thread, in which I posted KornShell functions for converting to and from Julian days (not to be confused with Julian dates, there's a link to an explanation).

Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
This has been answered a few times already see thread822-845095 it's worth having a look in the unix scripting forum as loads of these types of questions have already been asked

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top