in reply to Months between dates ?
While I agree with shadowsong that this logic would be better applied in the database, here is an SSCCE using only core modules and thus avoiding the heavy DateTime (which IMHO makes it "a better way" as you requested but that's a subjective call):
use strict; use warnings; use Time::Piece; use Test::More tests => 6; my $FMT = '%Y-%m-%d'; # ISO dates my @less = ('2018-03-01', '2019-07-20', '2019-08-07'); my @more = ('2020-03-01', '2019-09-20', '2019-08-20'); for (@less) { ok within_2_months ($_), "$_ is not more than 2 months in the futu +re"; } for (@more) { ok !within_2_months ($_), "$_ is more than 2 months in the future" +; } sub within_2_months { my $now = localtime (time); BAIL_OUT "Ach, you're too late. Should have run this earlier" if $now > Time::Piece->strptime ('2019-06-20', $FMT); my $then = Time::Piece->strptime (shift, $FMT); return $then < $now->add_months(2); }
|
|---|