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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.