comparing 2 dates

hi ,

I have two variables both containg dates,

x= `date`
and
y= `date'

their format being -> Fri Nov 12 22:59:50 MST 2004

how do I compare which one is greater.

->Can dates be converted into integer and then compared?

( one lengthy way would be to compare the words one by one (i.e., first compare year then month then day....) )
But is their a simpler way of

can any one help me please.

Thanks

See the FAQ by navigating
our home page -> Answers to Frequently Asked Questions -> Yesterdays Date/Date Arithmetic

Sure, use UNIX timestamps (ie, the number of seconds since 1970).

date +%s

date +%s

works for GNU date only ....

Unix is used loosely with both Unix and Linux the assumption goes with date +%s "unix"...
unless the original post specified otherwise.
what you are claiming is a little irrelevant its off topic and doesn't help the opriginal poster solve their problem..
do you have a better answer.. ifact do you know what flavour of Unix the original poster is uisng.. since linux is basically unix.

try gawk "substrings" to compare strings as you would in C just put the whole value of the time stamp in a variable and compare using the awk string comparison command..(awk is basically a language not a command so it will do the job just as you want) you will have to look it up in the man pages or try http://www.sourceforge.net to get the exact command since I am little rusty on awk..
moxxx68

Works for *BSD's date too.

thank you for your input...
that what I was questioning that proves my point..
date +%s is the relavent answer
moxxx68

I agree ...

I just want to go alternative which works in all flavours.

#!/usr/bin/ksh

a=`date "+%Y%m%d%H%M%S"`
echo $a
sleep 10
b=`date "+%Y%m%d%H%M%S"`
echo $b

if test $b -gt $a
then
echo "$b is greater than $a"
else
echo "$a is greater than $b"
fi