in reply to Adding Days To YYYYMMDD Date Format
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Adding Days To YYYYMMDD Date Format
by Tux (Canon) on May 17, 2012 at 07:36 UTC | |
|
Re^2: Adding Days To YYYYMMDD Date Format
by Zoomie (Novice) on May 25, 2012 at 15:19 UTC | |
|
Re^2: Adding Days To YYYYMMDD Date Format
by Anonymous Monk on Dec 05, 2018 at 11:22 UTC | |
by poj (Abbot) on Dec 05, 2018 at 12:35 UTC | |
|
Re^2: Adding Days To YYYYMMDD Date Format
by Zoomie (Novice) on May 17, 2012 at 16:07 UTC | |
by afoken (Chancellor) on May 17, 2012 at 16:31 UTC | |
by Kenosis (Priest) on May 17, 2012 at 19:23 UTC |