Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

How do I Compare Dates

by Anonymous Monk
on Jun 26, 2000 at 21:13 UTC ( #19878=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (dates and times)

given string representation of dates in the format dd-MMM-yyyy, coming from a database.

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I Compare Dates
by httptech (Chaplain) on Jun 26, 2000 at 21:51 UTC
    Date::Manip is particularly good at this. Look at the DateCalc() method.

    Alternatively, since you're getting the values from a database, see if you can get it to return the values in the format yyyy-mm-dd; then a simple string comparison will do. Or - heck - get it in nnnnnnnnn format, a number representing seconds since the epoch. Then a simple numeric comparison will do!

Re: How do I Compare Dates
by tenatious (Beadle) on Jun 27, 2000 at 04:21 UTC

    If the date you want to compare is in the same format as that you are pulling from the database, you might be able to perform a SELECT statement on it and let the database tell you which date is younger (or older).

    This method requires a connection to a database, and might not be the fastest way, but if you are designing for portability, would require less packages be installed than going whole hog and installing Date::Manip.

    You could also just compare the dates manually. I.e:

    ($d,$m,$y) = split /-/, $date; $dv1 = $d+31*$m+365*$y; ($d,$m,$y) = split /-/, $date_from_db; $dv2 = $d+31*$m+365*$y; if ($dv1 < $dv2) { something; ... ... done; }
Re: How do I Compare Dates
by KM (Priest) on Jun 26, 2000 at 21:26 UTC
    Take a peek at Date::Manip, particularly the DateCalc() method.

    Cheers,
    KM

    Originally posted as a Categorized Answer.

Re: How do I Compare Dates
by Shendal (Hermit) on Jun 26, 2000 at 21:27 UTC
    See Date::Manip. It does all sorts of date manipulation and calculation functions.

    Originally posted as a Categorized Answer.

Re: How do I Compare Dates
by davorg (Chancellor) on Jun 27, 2000 at 15:17 UTC

    Rather than getting the current date using `date`, why not use POSIX::strftime to get it in YYYY-MM-DD format. You can then use standard string comparison operators on the two dates.

    use POSIX 'strftime'; my $now = strftime('%Y-%m-%d', localtime);

    Date::Manip is probably overkill for this task (in fact it's probably overkill for most tasks.)

    Originally posted as a Categorized Answer.

Re: How do I Compare Dates
by Anonymous Monk on Jun 17, 2004 at 18:41 UTC
    Hello. For what it's worth, I wrote the following to be able to determine whether date1 is within 3 days of date2 etc. I didn't know about Date::Manip.
    sub abs_day { # this subroutine accepts a month, day, and two-digit year in # mm/dd/yy numerical form, e.g., 3/23/04. # it then calculates the "absolute day", # i.e., the number of days # since day 0, which is the turn of the 21st century. # so, 1/10/2000 is absolute day 10; # 1/10/2001 is absolute day 376 # (because year 2000 was a leap year). # the routine deals with leap years by # multiplying years by 365.25, and then rounding down. my $month = shift(@_); my $day = shift(@_); my $year = shift(@_); if ($debug) { print "i'm in abs_day and month is $month,", " day is $day, year is $year\n"; } my $abs_day; undef($abs_day); # so each year has 365.25 days # years are of the form 00, 01, 02, etc. $abs_day = $year * 365.25; # add .75 to account for fact that year 00 is a leap year $abs_day = $abs_day + .75; if ($debug) { print "abs_day is $abs_day after adding .75 to yr*365\n"; } if ($month == 1) #jan { $abs_day = $abs_day + 0; } if ($month == 2) #feb { $abs_day = $abs_day + 31; } # starting in march, we add .25 to number of days # to account for leap year if ($month == 3) #mar { $abs_day = $abs_day + 59.25; } if ($month == 4) #apr { $abs_day = $abs_day + 90.25; } if ($month == 5) #may { $abs_day = $abs_day + 120.25; } if ($month == 6) #jun { $abs_day = $abs_day + 151.25; } if ($month == 7) #jul { $abs_day = $abs_day + 181.25; } if ($month == 8) #aug { $abs_day = $abs_day + 212.25; } if ($month == 9) #sep { } if ($month == 11) #nov { $abs_day = $abs_day + 304.25; } if ($month == 12) #dec { $abs_day = $abs_day + 334.25; } $abs_day = $abs_day + $day; if ($debug) { print "now that we have added year, month, and", " day, abs_day is $abs_day\n"; } $abs_day = sprintf "%d", $abs_day; if ($debug) { print "now that we have converted from float to integer,", " abs_day is $abs_day\n"; } return $abs_day; }
Re: How do I Compare Dates
by djlerman (Sexton) on Jul 08, 2011 at 20:55 UTC
    for the answer provided by tenatious.

    Why does one need to calculate the date?
    $dv1 = $d+31*$m+365*$y; $dv2 = $d+31*$m+365*$y;
    Couldn't it just be:
    $dv1 = $yyyymmdd $dv2 = $yyyymmdd

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://19878]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2023-09-27 21:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?