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__
2013-01-01T00:00:00 :: 2013-01-31T00:00:00 ... main::via_pcouderc => 30 => $VAR1 = bless( { 'seconds' => 0, 'minutes' => 0, 'end_of_month' => 'wrap', 'nanoseconds' => 0, 'days' => 30, 'months' => 0 }, 'DateTime::Duration' ); 2013-01-01T00:00:00 :: 2013-01-31T00:00:00 ... main::via_choroba => 30 => $VAR1 = bless( { 'seconds' => 0, 'minutes' => 0, 'end_of_month' => 'wrap', 'nanoseconds' => 0, 'days' => 30, 'months' => 0 }, 'DateTime::Duration' ); 2013-01-01T00:00:00 :: 2013-02-03T00:00:00 ... main::via_pcouderc => 2 => $VAR1 = bless( { 'seconds' => 0, 'minutes' => 0, 'end_of_month' => 'wrap', 'nanoseconds' => 0, 'days' => 2, 'months' => 1 }, 'DateTime::Duration' ); 2013-01-01T00:00:00 :: 2013-02-03T00:00:00 ... main::via_choroba => 33 => $VAR1 = bless( { 'seconds' => 0, 'minutes' => 0, 'end_of_month' => 'wrap', 'nanoseconds' => 0, 'days' => 33, 'months' => 0 }, 'DateTime::Duration' ); 2013-01-01T00:00:00 :: 2014-01-02T00:00:00 ... main::via_pcouderc => 1 => $VAR1 = bless( { 'seconds' => 0, 'minutes' => 0, 'end_of_month' => 'wrap', 'nanoseconds' => 0, 'days' => 1, 'months' => 12 }, 'DateTime::Duration' ); 2013-01-01T00:00:00 :: 2014-01-02T00:00:00 ... main::via_choroba => 366 => $VAR1 = bless( { 'seconds' => 0, 'minutes' => 0, 'end_of_month' => 'wrap', 'nanoseconds' => 0, 'days' => 366, 'months' => 0 }, 'DateTime::Duration' );

In reply to Re^3: How to find the number of days with DateTime ? by Anonymous Monk
in thread How to find the number of days with DateTime ? by pcouderc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.