Bdfy
Bdfy

Reputation: 24621

How to print dates between 2 boundaries dates

I have 2 dates in format:

27.12.2007

12.03.2010

Howto print all days ( in cycle ) between this 2 dates ?

Upvotes: 2

Views: 2167

Answers (2)

Dave Cross
Dave Cross

Reputation: 69244

I know that you've already accepted an answer, but it uses the rather clunky old Date::Calc module. These days, most people would recommend using DateTime instead.

#!/usr/bin/perl

use strict;
use warnings;

use DateTime;

my %start;
@start{qw[day month year]} = split /\./, '27.12.2007';
my %end;
@end{qw[day month year]} = split /\./, '12.03.2010';

my $start = DateTime->new(%start);
my $end   = DateTime->new(%end);

my $curr = $start;

while ($curr <= $end) {
  print $curr->ymd, "\n";
  $curr->add(days => 1);
}

Upvotes: 5

Eugene Yarmash
Eugene Yarmash

Reputation: 149776

Use Date::Calc.

use Date::Calc qw(Delta_Days Add_Delta_Days);

my @begin = reverse split /\./, '27.12.2007';
my @end   = reverse split /\./, '12.03.2010';

my $delta = Delta_Days(@begin, @end); 

for my $i (0..$delta) {
    printf "%d-%02d-%02d\n", Add_Delta_Days(@begin, $i);
}   

Upvotes: 3

Related Questions