I understand that this is a veery old post. I needed a function to help me create a range of dates starting a few days ago and ending at the current date.
I played with different dates and I discovered that something wrong happens around October. The "date" is not incremented anymore after 20181028 or 20171029 or 20161030 and so on.
However, after that fatidic date it works fine again.
See and example:
Last date 20110101 Bill date 20181205 Data 20110102
Last date 20110101 Bill date 20181205 Data 20110103
Last date 20110101 Bill date 20181205 Data 20110104
.....
Last date 20110101 Bill date 20181205 Data 20111026
Last date 20110101 Bill date 20181205 Data 20111027
Last date 20110101 Bill date 20181205 Data 20111028
Last date 20110101 Bill date 20181205 Data 20111029
Last date 20110101 Bill date 20181205 Data 20111030
Last date 20110101 Bill date 20181205 Data 20111030
Last date 20110101 Bill date 20181205 Data 20111030
Last date 20110101 Bill date 20181205 Data 20111030
......
$last_date = <STDIN>;
chomp $last_date;
for (my $i = $last_date; $i <= $bill_date; $i = AddDate( $i, 1)) {
push @letter_dates, $i;
printf "Last date %s Bill date %s Data %s\n", $last_date, $bill_da
+te, $i;
}
sub AddDate {
my $date = $_[0];
my $x = $_[1]; # set this.
my $day = 24*60*60;
my $xdays = $x * $day;
my @t = (localtime(str2time($date) + $xdays))[5,4,3];
$t[0] += 1900; # localtime returns years since 1900.
$t[1] += 1; # localtime returns month in range 0..11.
$return_date = sprintf "%04d%02d%02d", $t[0], $t[1], $t[2];
return $return_date;
}
2018-12-06 Athanasius added paragraph and code tags