Perlwannabe wrote:
> 
> I have a relatively simple script that needs to get two separate dates,
> today's date, and yesterday's date.  The dates are in mmddyy format.
> Everything works great on days 2 - 31 of the month, but on the first of
> the month yesterday's date is not correct.  For example, on October 1,
> 2003 the variable $dateyesterday was "100003" when it needed to be 093003.
>  Obviously, there is no 100003.  Is there a way to deal with this problem
> on the first day of every month?

The easiest way is to get the results from the time() function which
returns the number of seconds since the epoch and subtract the number of
seconds in one day and pass that value to either the localtime() or
gmtime() function.


> ##################  BEGIN SCRIPT  ################################
> my ($mday, $mon, $year) = (localtime)[3..5];
> my $datetoday = sprintf ("%02d%02d%02d", ++$mon, $mday, $year-100);
                                                          ^^^^^^^^^
Why are you subtracting 100 from the year value returned from
localtime?  You do realize that if the year is 1999 or earlier the
result will be a negative number and if the year is 2100 or later the
result will be a three digit number?  To always get a two digit value
use modulo instead of subtraction.

my $datetoday = sprintf '%02d%02d%02d', $mon + 1, $mday, $year % 100;


> print ("the value of datetoday is $datetoday\n");
> 
> my ($yesterdaymday, $yesterdaymon, $yesterdayyear) = (localtime)[3..5];
> my $dateyesterday = sprintf ("%02d%02d%02d", ++$yesterdaymon,
> $yesterdaymday-1, $yesterdayyear-100);

# seconds in one day = 60 seconds * 60 minutes * 24 hours = 86400
seconds
my ( $yesterdaymday, $yesterdaymon, $yesterdayyear ) = (localtime time -
86400)[ 3 .. 5 ];
my $dateyesterday = sprintf '%02d%02d%02d', $yesterdaymon + 1,
$yesterdaymday, $yesterdayyear % 100;


> print ("the value of datetoday is $dateyesterday\n");


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to