Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Compare two dates

by ablanke (Monsignor)
on Jul 22, 2019 at 19:39 UTC ( [id://11103162]=note: print w/replies, xml ) Need Help??


in reply to Compare two dates

2 minor comments on your code
if ($d2 >= $1) {
i guess $1 is a typo.
print "\n $d2 is greater or equal to $d1\n";
your print statements are equal.

one recommended module on CPAN is DateTime

from the DateTime docs:
DateTime->compare( $dt1, $dt2 )
Compare two DateTime objects. The semantics are compatible with Perl's sort() function; it returns -1 if $d1 < $d2, 0 if $d1 == $d2, 1 if $d1 > $d2.
use strict; use warnings; use feature 'say'; use DateTime; my $d1 = DateTime->new( year => 2019, month => 8, day => 1, time_zone => 'America/Chicago', ); my $d2 = DateTime->new( year => 2019, month => 6, day => 8, time_zone => 'America/Chicago', );
say DateTime->compare( $d1, $d2 )
say DateTime->compare( $d1, $d2 ) > 0 ? $d1->ymd .' is greater than ' . $d2->ymd : $d2->ymd .' is greater or equal to ' . $d1->ymd;
update: sorry, my fault <no excuse>

Replies are listed 'Best First'.
Re^2: Compare two dates
by AnomalousMonk (Archbishop) on Jul 22, 2019 at 20:24 UTC
    your print statements are equal.

    There still seems to be a problem with the print statement because:

    c:\@Work\Perl\monks>perl -wMstrict -le "use feature 'say'; use DateTime; my $d1 = DateTime->new( year => 1999, month => 8, day => 1, time_zone => 'America/Chicago', ); my $d2 = DateTime->new( year => 2019, month => 6, day => 8, time_zone => 'America/Chicago', ); say DateTime->compare( $d1, $d2 ) ? $d1->ymd .' is greater than ' . $d2->ymd : $d2->ymd .' is greater or equal to ' . $d1->ymd; " 1999-08-01 is greater than 2019-06-08
    Maybe something like:
    c:\@Work\Perl\monks>perl -wMstrict -le "use feature 'say'; use DateTime; my $d1 = DateTime->new( year => 1999, month => 8, day => 1, time_zone => 'America/Chicago', ); my $d2 = DateTime->new( year => 2019, month => 6, day => 8, time_zone => 'America/Chicago', ); ;; my $comparison = DateTime->compare($d1, $d2); $comparison = $comparison > 0 ? 'greater than' : $comparison < 0 ? 'less than' : 'equal to'; ;; say sprintf '%s is %s %s', $d1->ymd, $comparison, $d2->ymd; " 1999-08-01 is less than 2019-06-08


    Give a man a fish:  <%-{-{-{-<

Re^2: Compare two dates
by soonix (Canon) on Jul 22, 2019 at 21:40 UTC
    This
    The semantics are compatible with Perl's sort() function; it returns -1 if $dt1 < $dt2, 0 if $dt1 == $dt2, 1 if $dt1 > $dt2.
    is not compatible with your code
    say DateTime->compare( $d1, $d2 ) ? $d1->ymd .' is greater than ' . $d2->ymd : $d2->ymd .' is greater or equal to ' . $d1->ymd;
    Better would be e.g.
    say $d1->ymd . ' is ' . ('earlier than', 'equal to', 'later than')[Dat +eTime->compare( $d1, $d2 ) + 1] . ' ' . $d2->ymd;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2024-04-18 14:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found