SUMMARY: RE: Comparing two date strings: any easy way?

From: Zaigui Wang <>
Date: Wed Jan 22 2003 - 02:53:26 EST
My apology first for this really late summary. In the
quest for a solution, I have been flexible enough to
modify the problem itself so that it ceased to be much
of a problem any more.

Needless to say that all the replies have been of
great help to my modifying/solving my problem, without
my realizing it, and all the posts obviously were
compiled with a lot of thoughts and efforts on the
part of posters. Some sent me java code, some perl
code, some gnu tool/utilities. I am posting some of
the replies I've receive verbatim here:

Tom Payerle: 
I like the Perl Time::ParseDate module from CPAN.  It
can handle a wide 
of input formats, plus specify what to do in
potentially ambiguous 
(e.g. should date without year be closest, last year
or next year).

Anyway, for a simple thing like you propose, the

#!/bin/perl -w
use strict;
use Time::ParseDate;

#Read 2 date strings from stdin
my $date1=<STDIN>;
my $date2=<STDIN>;
chomp $date1; chomp $date2;

#Convert to seconds since start of epoch
my $time1 = parsedate($date1);
my $time2 = parsedate($date2);

#Compute days difference
my $tdiff = $time2 - $time1;
my $ddiff = $tdiff/60/60/24;
print "Diff is $ddiff days\n";

Or if want to oneline it
perl -MTime::ParseDate -e '@d=<STDIN>;chomp @d;print 
Dan Lorenzini
(use gnu date, most linux distributions have gnu date.
It is also available from gnu as part of the sh-utils

	$ D1=`gdate +%s -d "Tue Nov 19 08:56:06 PST 2002"`
	$ D2=`gdate +%s -d "Mon Jan  6 15:43:59 PST 2003"`
	$ echo $D1 $D2
	1037724966 1041896639
	$ DAYS=`expr \( $D2 - $D1 \) / 86400`
	$ echo $DAYS

expr will only give you integers.  If you want
decimals, you can use 

	$ echo $D1 $D2 | awk '{print ($2-$1)/86400}'

gdate is on the companion CD with solaris 8 and above.
 Hope this 
Chris Keladis:
Not anything built-in, but you could use a GNU'ish
date command that 
the '-r' option to take the Epoch and return a string
again (and `date 
+%s` to get the time now as Epoch).

To do it in perl you would either need to roll your
own str2time() and 
time2str() functions, or use the ones in the
Date::Parse, or 
CPAN modules.

Sorry, in my reply i was mixing perl and sh to do what
you asked, but 
could also be done solely in either language.

Lupe Christoph
(perl module Date::Manip)

% perl -MDate::Manip -e 'print
<>), \
    ParseDate(scalar <>)),0,"%dt"), "\n";'
Tue Nov 19 08:56:06 PST 2002
Mon Jan  6 15:43:59 PST 2003

-- ----------
I also found this interesting post:

Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
sunmanagers mailing list
Received on Wed Jan 22 10:09:55 2003

This archive was generated by hypermail 2.1.8 : Thu Mar 03 2016 - 06:43:02 EST