in reply to Re: How do I find the difference in days between two dates, in a cool perl way?
in thread How do I find the difference in days between two dates, in a cool perl way?

I believe this is incorrect. print $duration->days; Should be print $duration->delta_days; if your delta is greater than or equal to a calendar week.
  • Comment on Re: Answer: How do I find the difference in days between two dates, in a cool perl way?

Replies are listed 'Best First'.
Re^2: Answer: How do I find the difference in days between two dates, in a cool perl way?
by hippo (Archbishop) on Jul 12, 2016 at 14:53 UTC

    Anonymous Monk is correct.

    #!/usr/bin/env perl use strict; use warnings; use Test::More tests => 4; use DateTime; my $dt1 = DateTime->new ( year => 2015, month => 6, day => 1 ); my $dt2 = DateTime->new ( year => 2015, month => 6, day => 30 ); my $duration = $dt1->delta_days ($dt2); is $duration->days, 29, 'Without delta, more than a week'; is $duration->delta_days, 29, 'With delta, more than a week'; $dt2 = DateTime->new ( year => 2015, month => 6, day => 3 ); $duration = $dt1->delta_days ($dt2); is $duration->days, 2, 'Without delta, less than a week'; is $duration->delta_days, 2, 'With delta, less than a week';

    Test 1 fails for me (with DateTime 1.20) but the others all pass. This fits with the description of the days() method on a duration which says:

    These methods return numbers indicating how many of the given unit the object represents, after having done a conversion to any larger units. For example, days are first converted to weeks, and then the remainder is returned.