Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How to find the number of days with DateTime ?

by pcouderc (Monk)
on Jan 03, 2014 at 10:52 UTC ( [id://1069120]=perlquestion: print w/replies, xml ) Need Help??

pcouderc has asked for the wisdom of the Perl Monks concerning the following question:

I would like to get the number of days between 2 dates. with DateTime. I have tried manythings including :
my $dt=$datec->subtract_datetime( $dateref )->delta_days();
or :
my $dt=$datec->clone->subtract_datetime( $dateref ); $dt=$dt->in_units('days');
but when more that 1 montth, $dt remains less than 30.
Than you in advance, o precious monks.

Replies are listed 'Best First'.
Re: How to find the number of days with DateTime ?
by choroba (Cardinal) on Jan 03, 2014 at 11:05 UTC
    The following script returns 295, which is greater than 30. No subtract_datetime is needed.
    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use DateTime; my $dateref = 'DateTime'->new( year => 2013, month => 3, day => 14, ); my $datec = 'DateTime'->now; my $dt = $datec->delta_days($dateref); say $dt->in_units('days');
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thenk you very much. It works very fine ! Why make simple han you can mame complicated (and wrong...) ?

        subtract_datetime() calculates the difference in days & months but you choose only the day difference and ignore the month difference. And, delta_days(), per pod of installed version here, takes a DateTime object unlike your usage. in_units() seems screwy until one gets the doc linked there.

        use strict; use warnings; use Data::Dumper; local $Data::Dumper::Indent = 2 ; local $Data::Dumper::Pad = ' ' ; local $Data::Dumper::Deepcopy = 1 ; use DateTime; my %base = ( 'hour' => 0 , 'minute'=> 0 , 'second' => 0 , 'time_zone' => +0000 ); my $ref_date = DateTime->new( %base , 'year' => 2013 , 'month' => 1 , 'day' => 1 +); my @date = ( DateTime->new( %base , 'year' => 2013 , 'month' => 1 , 'day' => 31 + ) , DateTime->new( %base , 'year' => 2013 , 'month' => 2 , 'day' => 3 + ) , DateTime->new( %base , 'year' => 2014 , 'month' => 1 , 'day' => 2 + ) ); for my $date ( @date ) { for my $method ( \&via_pcouderc , \&via_choroba ) { printf "%s :: %s ...\n" , map $_->iso8601(), $ref_date , $date; $method->( $ref_date , $date ); print "\n"; } print "\n"; } exit; sub via_pcouderc { my ( $one , $two ) = @_; my $obj = $two->subtract_datetime( $one ); return show( $obj , $obj->delta_days() , ( caller(0) )[3] ); } sub via_choroba { my ( $one , $two ) = @_; my $obj = $two->delta_days( $one ); return show( $obj , $obj->in_units('days') , ( caller(0) )[3] ); } sub show { my ( $interval , $result , $sub ) = @_; printf "%s => %s\n => %s\n" , $sub , $result , Dumper( $interval ); return; } __END__
Re: How to find the number of days with DateTime ?
by Jim (Curate) on Jan 03, 2014 at 21:07 UTC
Re: How to find the number of days with DateTime ?
by vagabonding electron (Curate) on Jan 04, 2014 at 14:37 UTC
      Thank you all.
      Yes, DateTime::Moonpig seems fine.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1069120]
Approved by Corion
Front-paged by Jim
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-24 02:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found