Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Compare two dates

by harangzsolt33 (Chaplain)
on Jul 22, 2019 at 21:03 UTC ( [id://11103169]=note: print w/replies, xml ) Need Help??


in reply to Compare two dates

Your code has some bugs in it.. I would do it like this :

use strict; my $d1 = '2019-06-08'; my $d2 = '2019-06-08'; my $RESULT = $d2 cmp $d1; if ($RESULT > 0) { print "\n$d1 is smaller.\n"; } elsif ($RESULT < 0) { print "\n$d1 is larger.\n"; } else { print "\nThe dates are equal!\n"; }

Replies are listed 'Best First'.
Re^2: Compare two dates
by Laurent_R (Canon) on Jul 22, 2019 at 22:54 UTC
    Yeah, that's correct, but it is slightly more complicated than it needs to be. Using the lt and gt string comparison operators is a bit simpler:
    my $d1 = '2019-06-08'; my $d2 = '2019-06-08'; if ($d1 lt $d2) { print "\n$d1 is smaller.\n"; } elsif ($d2 gt $d1) { print "\n$d1 is larger.\n"; } else { print "\nThe dates are equal!\n"; }
    Or:
    my $d1 = '2019-06-08'; my $d2 = '2019-06-08'; print $d1 lt $d2 ? "$d1 smaller" : $d1 gt $d2 ? "$d1 larger" : "Dates are equal";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2024-03-28 11:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found