in reply to Counting days with DateTime

Personally I try to avoid the overloaded DateTime math operators, and only use the comparisons. I'd suggest something like:

my $now = DateTime->now( time_zone => 'Pacific/Honolulu' ); my $one_week_from_now = $now->clone->add( weeks=>1 ); if ( $reservation_date > $one_week_from_now ) ...

Update: Corrected ->add( week=>1 ) to ->add( weeks=>1 )!

Update 2: Added ->clone, as suggested by tobyink below. That's what I get for running a different test case (DateTime->now->add(weeks=>1)) than what I posted here :-/

Replies are listed 'Best First'.
Re^2: Counting days with DateTime (updated)
by tobyink (Canon) on Apr 02, 2020 at 07:18 UTC

    Consider using:

    my $one_week_from_now = $now->clone->add( weeks=>1 );

    This is because most of the DateTime add/subtract methods mutate the object the operated on, and you probably don't want $now to be a date in the future!

Re^2: Counting days with DateTime
by htmanning (Friar) on Apr 01, 2020 at 19:33 UTC
    Thanks so much! This worked after I changed "week" to "weeks."
    A reply falls below the community's threshold of quality. You may see it by logging in.