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

$date = ParseDate("yesterday"); How to get 3 days ago date from current date.

Replies are listed 'Best First'.
Re: 3 days previous Date
by ikegami (Patriarch) on Jun 15, 2010 at 05:35 UTC

    That looks like Date::Manip. I'm not familiar with that module and the docs aren't straightforward, so I'm going to give an answer using the module I use: DateTime.

    my $dt = DateTime->today->subtract( days => 3 );
      I used like this in my one of the script
      #!/usr/bin/perl sub _timestamp { my $diff = shift; my ($sec,$min,$hour,$mday,$mon,$year,$wday) = localtime(time - $di +ff); $mon++; return sprintf("%d-%02d-%02d",($year + 1900),$mon,$mday); } $pre=259200; # 24*60*3 $myTimeStamp = _timestamp ($pre); print "$myTimeStamp \n";
      bharat

        Aside from

        $pre=24*60*60*3;
        being much clearer than
        $pre=259200; # 24*60*3

        not every day has 24*60*60 seconds. Your code is buggy.

Re: 3 days previous Date
by Your Mother (Archbishop) on Jun 15, 2010 at 05:51 UTC

    Date::Manip, though I don't think this is really the way to go. DateTime is much more flexible and is quite good at date math.

    perl -MDate::Manip -le 'print UnixDate(ParseDate("3 days ago"), "%A")' # Friday
Re: 3 days previous Date
by JavaFan (Canon) on Jun 15, 2010 at 07:37 UTC
    There are many date/time packages on CPAN, most of them perfectly capable of adding/subtracting dates. For simple date calculations, I prefer Date::Simple:
    $ perl -MDate::Simple=:all -wE 'say today() - 3' 2010-06-12 $
Re: 3 days previous Date
by stevieb (Canon) on Jun 15, 2010 at 14:32 UTC

    update: sorry ikegami, somehow I completely overlooked your post ;)

    Personally, I like DateTime for all date functions. It is extremely flexible

    #!/usr/bin/perl use warnings; use strict; use DateTime; my $date = DateTime->now(); print $date->ymd() . "\n"; $date->subtract( days => 3); print $date->ymd() . "\n";
    Steve
Re: 3 days previous Date
by ungalnanban (Pilgrim) on Jun 15, 2010 at 05:42 UTC
    Try with the following example code
    use strict; use warnings; my $current = `date`; + print "Current date and time is $current"; print `date -d "1 day ago"`; print `date -d "1 week ago"`; print `date -d "1 month ago"`; print `date -d "1 year ago"`;

      Running a shell so many times sounds expensive and not portable.