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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to compare 2 date ?

Status
Not open for further replies.

abruzzi

Programmer
Mar 27, 2007
17
FR
hello,

I've made this script
Code:
#!/usr/bin/perl -w


use strict;
use warnings;
use English;
use POSIX qw(strftime);
my $madate1             = strftime( '%d/%m/%Y %H:%M:%S', localtime );
my $madate='22/10/2008 12:00:00';
if ($madate le $madate1)
{
print "ok\n";
}
else
{
print "ko\n";
}

he doesn't run good
he doesn't compare the 2 dates correctly
it returns me ok or 2007 is less than 2008

i don't understand ?

Thanks
 
IF you're going to compare dates using strings, then you need to have a string format that is amenable to comparison:
YYYYMMDDHHMMSS

But this seems a little backwards to the way I would work.
I would convert your $madate into it's equivalent epoch date, and just compare that to localtime.

Or... use Date::Calc (I believe)
 
i can't use use Date::Calc
how can i do to convert $madate into it's equivalent epoch date ?
 
Sorry. I was being redundant. I would have used Date::Calc.

But, if you're sticking with strings, then format your string as I proposed: YYYYMMDDHHMMSS e.g.--> 20070425110903

Use 4 digit dates, leading zeroes on all fields, and 24-hour notation, and you can then compare the strings.
 
you an convert the dates into the format brigmar has shown you and compare the dates as strings, or use the Time::Local module, which comes with perl, to convert dates to epoch time and compare them as digits using math operators like:

>=
<=
>
<
==
!=



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Actually, because the format I suggested contains just numberals, it can be compared using the math operators too.
 
Actually, because the format I suggested contains just numberals, it can be compared using the math operators too.

true dat [smile]



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top