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

Hi Monks,

I wanted to ask you if there is a way to calculate the difference in days between 3 or more dates, at once. I know that for two days you would do:

use strict; use warnings; use Date::Calc qw(:all); my $date_first = '2005-09-27'; my $date_second = '2019-12-22'; my @array_first = split(/-/, $date_first); my @array_second = split(/-/, $date_second); my $dd = Delta_Days(@array_first,@array_second); print "$dd\n";

Is there a way to expand it to take 3, or do I need to calculate the distances separately, in pairs of 2?

Replies are listed 'Best First'.
Re: Calculate difference between multiple days
by choroba (Cardinal) on Oct 24, 2018 at 15:27 UTC
    What do you mean by "the difference in days between 3 dates"? What answer do you expect for, e.g., 2005-09-27, 2019-12-22, and 2018-10-24?

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Calculate difference between multiple days
by hippo (Archbishop) on Oct 24, 2018 at 15:35 UTC

    Assuming you want days between date 1 and date 2 and between date 2 and date 3 etc. then it's a simple enough loop:

    #!/usr/bin/env perl use strict; use warnings; use Date::Calc 'Delta_Days'; my @dates = ('2005-09-27', '2019-12-22', '2018-10-24', '1999-12-31'); my @start = split (/-/, shift @dates); while (0 < @dates) { local $" = '-'; my @end = split (/-/, shift @dates); print "@start to @end is " . Delta_Days (@start, @end) . " days\n" +; @start = @end; }
Re: Calculate difference between multiple days
by Discipulus (Canon) on Oct 24, 2018 at 15:45 UTC
    Hello, I doubt to have understood what a difference from 3 dates can be..

    I just guess you want the maximum difference in a set; if so you can sort by epoch and take first and last one:

    use strict; use warnings; use Date::Calc qw(:all); my @array1 = (2018,04,11,0,0,0); my @array2 = (2018,04,12,0,0,0); my @array3 = (2018,04,13,0,0,0); my @ordered = sort { Mktime(@{$a})<=> Mktime(@{$b})} \@array1,\@array2 +,\@array3; print "@$_\n" for @ordered; my $dd = Delta_Days(@{@ordered[0]}[0..2],@{@ordered[-1]}[0..2]); print "Max delta is $dd day(s)\n"; #OUTPUT 2018 4 11 0 0 0 2018 4 12 0 0 0 2018 4 13 0 0 0 Max delta is 2 day(s)

    If not, please explain your question more verbosely

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Hello all and thank you for your kind replies!
      I actually meant an all-against-all comparison, but I think I will go for something like this:
      #!/usr/bin/env perl use strict; use warnings; my $date_first = '2005-09-27'; my $date_second = '2019-12-22'; my $date_third = '1999-12-22'; use Time::Piece; my $date1 = localtime( $date_first ); my $date2 = localtime( $date_second ); my $date3 = localtime( $date_third ); if( $date2 > $date1 and $date2 > $date3 and $date1 > $date3) { print "correct\n"; }

      since I just needed to only compare between them and not calculate days. This works for me now, thanks again!
        if( $date2 > $date1 and $date2 > $date3 and $date1 > $date3)

        Can you spot which of these three comparisons is redundant?