Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Counting days with DateTime (updated x2)

by haukex (Archbishop)
on Apr 01, 2020 at 19:06 UTC ( [id://11114923]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-25 13:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found