Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Date comparison

by bengmau (Beadle)
on Jul 05, 2002 at 16:40 UTC ( #179677=perlquestion: print w/replies, xml ) Need Help??

bengmau has asked for the wisdom of the Perl Monks concerning the following question:

I have 4 strings with date "02/03/08" "02/04/02" I want to compare dates to see which is equal, newer etc.. same for time "3:00pm" and "4:00am" What is the easiest way to compare time and dates?

Replies are listed 'Best First'.
Re: Date comparison
by ehdonhon (Curate) on Jul 05, 2002 at 16:49 UTC

    Take a look at Date::Parse. You can use that to convert them to unix timestamp, and then just do numeric comparison.

      ehdonon ++ this is by far the easiest solution, and worth spelling out so that it doesn't get lost in the subsequent controversy
      use Date::Parse; my @dates = qw[ 02/03/08 02/04/02 3:00pm 4:00am ]; my @sorted = sort {str2time($a) <=> str2time($b)} @dates; print $_,"\n" for @sorted; __END__ 02/04/02 4:00am 3:00pm 02/03/08
      - n.b. times are assumed to be *today* unless date is specified. This will handle a variety of different date / time formats.

      George Sherston
        Since str2time() is a relatively expensive routine, this a perfect time for pulling out the "Orcish Maneuver".

        In the example above, change   my @sorted = sort {str2time($a) <=> str2time($b)} @dates; to

        my %cache; my @sorted = sort {($cache{$a} ||= str2time($a)) <=> ($cache{$b} ||= str2time($b)) } @dates;

Re: Date comparison
by Abigail-II (Bishop) on Jul 05, 2002 at 16:50 UTC
    Use Date::Calc, or if that can't parse it, or if you need very specialized date functions, use Date::Manip.

    Abigail

      I tried Date::Manip.. module is too big! I'll try Date parse.. see what I can see..
        In my version of Perl Date::Manip weighs in at 211KB which is 1KB less than the wildly popular CGI
        What do you mean by "module is too big"? Perl doesn't have any limitations on how large a module can be.

        Abigail

Re: Date comparison
by cjf (Parson) on Jul 05, 2002 at 17:17 UTC

    Date::Calc should do the job. For your first situation with dates in year/month/day format:

    my $string_one = "02/03/08"; # assuming year/month/day my $string_two = "02/04/02"; my @date_one = split /\//, $string_one; my @date_two = split /\//, $string_two; $date_one[0] = 20 . $date_one[0]; $date_two[0] = 20 . $date_two[0]; my $time_one = Date_to_Time(@date_one, 0, 0, 0); # 0s are for hour/min +/sec my $time_two = Date_to_Time(@date_two, 0, 0, 0); if ($time_one > $time_two) { print "$time_one is newest.\n"; } elsif ($time_one < $time_two) { print "$time_two is newest.\n"; } else { print "Same date (to the nearest day).\n"; }

    Comparing dates in hour:minuteAM/PM should be simple as well, just split the string for the hour, minute, and AM/PM parts, add 12 to the hours if it contains PM, convert to minutes, and compare.

Re: Date comparison
by joshua (Pilgrim) on Jul 05, 2002 at 22:40 UTC
    If you want a module-free way to compare dates, you could look here.

    Joshua

Re: Date comparison
by tomhukins (Curate) on Jul 05, 2002 at 17:32 UTC
    Others have asked and answered this question here before. Typing date into the search box brings up some relevant posts. You could use Super Search if you require more precise information.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2021-11-29 03:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?