05.19.08

Loop through dates in perl

Posted in programming at 23:51 by dalore

A little code tidbit, how to loop through dates a day at a time, can easily be modifed for other time periods but this is all I needed:

#!/usr/bin/perl -w

use Date::Calc qw(Add_Delta_Days Delta_Days);

my @date = (2008, 1, 1);
my @enddate = (2009, 1, 1);

while (Delta_Days(@date, @enddate) >= 0)
{
  print "$date[0]-$date[1]-$date[2]\n";
  #... do stuff

  # move to next day
  @date = Add_Delta_Days(@date, 1);
}

Leave a Comment

*
To prove that you're not a bot, enter this code
Anti-Spam Image