in reply to DateTime issues

#!/usr/bin/perl -CSDA use utf8; use Modern::Perl; use Time::Piece; use Time::Seconds; my $reservation_date = localtime->strptime("2020-03-15#", "%Y-%m-%d#") +; print "$reservation_date\n"; my $one_year_from_now = $reservation_date->add_months(-12); if ($reservation_date > $one_year_from_now) { printf "%s is after one year earlier from now that is %s\n", $reservation_date->strftime("%Y-%m-%d"), $one_year_from_now->strftime("%Y-%m-%d"), } my $one_week_from_now = localtime() + ONE_WEEK; if ($reservation_date < $one_week_from_now) { printf "%s is before one week from now that is %s\n", $reservation_date->strftime("%Y-%m-%d"), $one_week_from_now->strftime("%Y-%m-%d %H:%M:%S"), }
result: Sun Mar 15 00:00:00 2020 2020-03-15 is after one year earlier from now that is 2019-03-15 2020-03-15 is before one week from now that is 2020-04-12 20:43:49

Replies are listed 'Best First'.
Re^2: DateTime issues
by haukex (Archbishop) on Apr 06, 2020 at 06:13 UTC
    my $reservation_date = localtime->strptime("2020-03-15#", "%Y-%m-%d#") +; my $one_year_from_now = $reservation_date->add_months(-12); if ($reservation_date > $one_year_from_now) {

    That's a confusing name for that variable, and also that comparison will always be true.