Quote:
Originally Posted by joeferns79
Is there a way of getting the previous date when we enter a specific date?
e.g if we enter ... 20090804, it should return 20090803, or even if we enter a future date...20090910, it should return 20090909.
You can use one of the Date modules listed on CPAN or you can write your own code to split the date into tokens (year, month, day) and feed them to Time::Local ( that comes with perl) to convert them into epoch seconds and subtract 24 hours of seconds from the epoch time.
Proof of concept:
-
use Time::Local;
-
-
my $date = '20090910';
-
my ($y,$m,$d) = unpack("A4A2A2",$date);
-
my $epoch = timelocal(0,0,0, $d, $m-1, $y) - (60*60*24);
-
print scalar localtime($epoch);
-
assumes the date is always in YYYYMMDD format, if not you will have to change unpack() to a regexp and do some checking. You can use POSIX to reformat $epoch back into YYYYMMDD. I leave that part up to you. Or as mentioned, look into a Date module.