Skip to content

Instantly share code, notes, and snippets.

@kwakwaversal
Last active September 18, 2017 14:26
Show Gist options
  • Save kwakwaversal/0c705bae7bc9bb7553f6 to your computer and use it in GitHub Desktop.
Save kwakwaversal/0c705bae7bc9bb7553f6 to your computer and use it in GitHub Desktop.
Returns an array of dates #perl
=head2 get_date_range $start, $end
Returns a list of dates from the range in YYYY-MM-DD format.
$start and $end must also be in the same format. This is assumed and will break
if the format is anything other than that.
my $now = DateTime->now()->ymd;
my $last_month = DateTime->now()->subtract( days => 51 )->ymd;
my $date_range = get_date_range($last_month, $now, 1);
=cut
sub get_date_range {
my ($start, $end, $subtract_days) = @_;
croak "Insufficient arguments!" unless $start && $end;
# 2010-01-01 to 2010-02-01
my @sbits = split /-/, $start;
my @ebits = split /-/, $end;
my $start_date = DateTime->new( year=>$sbits[0], month=>$sbits[1], day=>$sbits[2]);
my $end_date = DateTime->new( year=>$ebits[0], month=>$ebits[1], day=>$ebits[2]);
$end_date->subtract( days => $subtract_days ) if $subtract_days;
my @date_range;
while ($start_date <= $end_date) {
push @date_range, $start_date->ymd;
$start_date->add( days => 1 );
}
return \@date_range;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment