Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

If i got a date from a database that is in a format of 11/24/99 is there a way to get the date of say 10 day before??

ie.
lastdate = 11/24/99
firstdate = lastdate - 10 ?? (doesn't work btw.)
trying to get 11/14/99

Edit by tye to preserve formatting and because single-word titles complicate future simple searches

Replies are listed 'Best First'.
Re: Date
by chromatic (Archbishop) on Feb 20, 2000 at 05:41 UTC
Re: Date
by Gollum (Initiate) on Jul 21, 2000 at 16:17 UTC
    A small variation on the solution offered above would be to use the inbuilt function time to generate the current date in seconds, then subtract 10 days (in seconds) from the result (as described above), and finally put the result into a calendar friendly format using localtime. You could use strftime (as I have done) to further format the result.

    use POSIX qw(strftime);

    my $thisday = time;
    my $tenDaysPrevious = $thisday - 10*24*60*60;
    $tenDaysPrevious = strftime "%m/%d/%Y", (localtime($tenDaysPrevious));

Re: Date
by Anonymous Monk on Feb 19, 2000 at 11:56 UTC
    For that, you're probably going to have to use localtime() and timelocal(). localtime() is a built-in function that takes a time - number of seconds since January first, 1970 - and returns an array that looks like:

    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)

    timelocal() is part of the perl module Time::Local. It does the opposite of localtime, taking an array and returning the number of seconds since 1970. The easiest way I can think of to do what you want would be to extract the day, month, and year from your date, timelocal() it to get a number of seconds, subtract the number of seconds in ten days (10 days * 24 hours * 60 minutes * 60 seconds), then localtime it to get day, month, and year again. Something like this:

    $date = (undef, undef, undef, $day, $month, $year);
    $tenDaysPrevious = $date - 10*24*60*60;
    (undef, undef, undef, $laterDay, $laterMonth, $laterYear) = timelocal($tenDaysPrevious);

    See? Easy.
      would u help me on finding the yesterday date.I have tried using the use::Format qw(time2str). And i want to run a file where to generate the data for last month. Infact all the possible date format i tried are not working. Please help.