The right way to do this is to parse it into a number of seconds as would be returned by time() and then to add the number of seconds in a day times the number of days you want, and then convert it back.
use Date::Parse;
my $day = 24*60*60;
my $x = 3; # set this.
my $xdays = $x * $day;
print scalar localtime(str2time("20121231") + $xdays);
Update: Changed to produce the requested format and to use the given example. Apparently I have too much time on my hands.
use Date::Parse;
my $date = "20120516";
my $day = 24*60*60;
my $x = 24; # set this.
my $xdays = $x * $day;
# see perldoc -f localtime.
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.
printf "%04d%02d%02d\n", @t;
-sauoq
"My two cents aren't worth a dime.";
-
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.
|