Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return all dates between to dates

Status
Not open for further replies.

r2d22y

Technical User
Nov 30, 2004
46
SE
Hello

I would be very glad if anyone could help me to solve this problem.

I have a "start date" and an "end date" now I need to get all dates between those days stored in an array (@dates)...has all the dates included ("start date" .. all dates between.. "end date")

How is that done using PERL?

Regards/D_S

 
Here's one suggestion, based on the Date::Calc module:
Code:
#!/usr/bin/perl -w
use strict;
use Date::Calc qw(:all);

# set start and end dates
my $start_date = '2005-04-21';
my $end_date = '2006-01-01';

# this will store our result
my @all_dates;

# we'll keep adding days to this until we hit the end date
my @to_check = split /-/, $start_date;

# this is the date to end at, in usable format
my @last = split /-/, $end_date;

# while we're before the end date
while( Delta_Days( @to_check, @last ) >= 0 ) {
   # add to @all_dates
   push @all_dates,  sprintf( "%04d-%02d-%02d", @to_check );

   # go onto the next day
   @to_check = Add_Delta_Days( @to_check, 1 );
}

# print 'em all out.
print "$_\n" for @all_dates;
 
Thank you very much, it works perfectly!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top