in reply to Re: How to find the number of days with DateTime ?
in thread How to find the number of days with DateTime ?

Thenk you very much. It works very fine ! Why make simple han you can mame complicated (and wrong...) ?
  • Comment on Re^2: How to find the number of days with DateTime ?

Replies are listed 'Best First'.
Re^3: How to find the number of days with DateTime ?
by Anonymous Monk on Jan 03, 2014 at 14:18 UTC

    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__
      Could somebody please either hide the Perl code in "readmore" tags and remove "readmore" from the output; or hide both parts? Rational is that output is more important, for the OP simply needs to spend quality time with DateTime to understand it.