Hi, so when you got the error, did you print out the value of $date to see what you were passing to the parser?

The first answer you got had all the tools you needed. Here I've added Test::More so I can set up some simple testing of my subroutine, checking comparisons that I already know the answer to. It's a good way to develop your code.

use strict; use warnings; use feature 'state'; use Test::More; use DateTime::Format::Strptime; for my $line (<DATA>) { chomp $line; my ( $first, $second, $expected ) = split /,/, $line; is( date_cmp($first, $second), $expected, "compared to $first, $se +cond is $expected" ); } done_testing; exit; sub date_cmp { state $parser = DateTime::Format::Strptime->new(pattern => '%m/%d/ +%Y'); my $dt1 = $parser->parse_datetime(shift) or die $parser->errmsg; my $dt2 = $parser->parse_datetime(shift) or return 'invalid'; return 'equal' if $dt1 eq $dt2; return $dt1 > $dt2 ? 'earlier' : 'later'; } __DATA__ 03/02/1999,03/01/1999,earlier 03/02/1999,03/03/1999,later 03/02/1999,03/02/1998,earlier 03/02/1999,03/02/2000,later 03/02/1999,03/02/1999,equal 03/02/1999,03/02/199 ,invalid
Output:
$ perl dt.pl ok 1 - compared to 03/02/1999, 03/01/1999 is earlier ok 2 - compared to 03/02/1999, 03/03/1999 is later ok 3 - compared to 03/02/1999, 03/02/1998 is earlier ok 4 - compared to 03/02/1999, 03/02/2000 is later ok 5 - compared to 03/02/1999, 03/02/1999 is equal ok 6 - compared to 03/02/1999, 03/02/199 is invalid 1..6

Hope this helps!


The way forward always starts with a minimal test.

In reply to Re^3: How to compare two dates? by 1nickt
in thread How to compare two dates? by CountOne

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.