http://qs321.pair.com?node_id=88053

ganesh_saravanan has asked for the wisdom of the Perl Monks concerning the following question: (dates and times)

How do i find yesterday's date

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do i find yesterday's date
by footpad (Abbot) on Jun 13, 2001 at 20:57 UTC

    The simple answer? See perlfaq4.

    The following provides examples (and a tweak) to the answers found there. It also shows different ways to use Date::Calc and Date::Manip.

    #!/usr/bin/perl -w use strict; use Date::Calc ( ":all" ); use Date::Manip; my ( $date, $yy, $dd, $mm ); print "There are various ways to get yesterday's date. Here are\n", "are a few alternatives and their results:\n\n"; # perlfaq4 - simple $date = scalar localtime( ( time() - ( 24 * 60 * 60 ) ) ); print "1. The simple calculation in perlfaq4 yields: ", "$date.\n\n"; # perlfaq4 - DST aware $date = scalar localtime( yesterday() ); print "2. The DST subroutine in perlfaq4 reports: ", "$date.\n\n"; # One way using Date::Calc ( $yy, $mm, $dd ) = Today(); ( $yy, $mm, $dd ) = Add_Delta_Days( $yy, $mm, $dd, -1 ); $mm = Month_to_Text( $mm ); print "3. A simple use of Date::Calc gives: ", "$dd $mm $yy.\n\n"; # Date::Manip; note that the timezone is my local; # change as needed. $ENV{TZ} = "PST8PDT"; $date = ParseDate( "yesterday" ); print "4. Date::Manip returns $date, as well as ", UnixDate( "yesterday", "%e %b %Y"), "\n\n"; print "Other approaches are certainly possible.\n"; exit 1; sub yesterday { # Borrowed from perlfaq4; note changes below. my $now = defined $_[0] ? $_[0] : time; my $then = $now - 60 * 60 * 24; my $ndst = (localtime $now)[8] > 0; my $tdst = (localtime $then)[8] > 0; # Added '=' to avoid warning (and return) $then -= ($tdst - $ndst) * 60 * 60; return $then }

    --f

Re: How do i find yesterday's date
by marcink (Monk) on Jun 13, 2001 at 17:48 UTC
    Lots of ways; the easiest is to use 'time' function (print gmtime( time - 24 * 60 * 60 )). You might also check Date::Manip module -- its ParseDate function allows for specifying time in quite complex ways (like "3 days ago".

    use Date::Manip; print ParseDate( "yesterday" ) . "\n";


    -mk
Re: How do i find yesterday's date
by monsterzero (Monk) on Nov 01, 2005 at 17:28 UTC
    You can use DateTime for this
    use DateTime; my $dt = DateTime->now()->subtract( days => 1 );
Re: How do i find yesterday's date
by Pedro Picasso (Sexton) on Jun 13, 2001 at 18:08 UTC
    Date::Calc also offers a lot of fun functions including adding and subtracting days from a date and getting the date of a monday of any given week.
Re: How do i find yesterday's date
by laurentschneider (Initiate) on Jun 09, 2004 at 10:47 UTC
    To bypass the dst problem, you can do "this morning at 0:00 minus twelve hours".
    @T=localtime(time-time%86400-43200); printf("%02d/%02d/%02d",$T[4]+1,$T[3],($T[5]+1900));
    This works only for dates < 19-JAN-2038 (which is 2 ^ 31 in perl). Regards Laurent Schneider

      This question has been answered in perlfaq4 using Date::Calc and working with use strict.

      »Jan 19 2038« is not perl's problem. It depends on your libc and/or your 32/64 bit processor. With these problems your code only works until 2**31 - 1 (at least at my machine).

      -- Frank

      As was pointed out to me some time ago, be careful with assumptions about length of day. There are, I think up to three opportunities per year for there to not be 24 hours of 60 minutes of 60 seconds in a day.

      I'd like to point out that this solution will also not work when the timezone is 12 or more hours on either side of GMT (I believe there are some that are off by 13 hours). It's probably best to stick with Date::Calc.
      $ date; TZ=GMT0 date Wed Jun 9 12:01:15 EDT 2004 Wed Jun 9 16:01:15 GMT 2004 $ TZ=GMT-12 perl foo.pl 06/09/2004 $ TZ=GMT+12 perl foo.pl 06/08/2004 $ TZ=GMT-13 perl foo.pl 06/09/2004 $ TZ=GMT+13 perl foo.pl 06/07/2004
Re: How do i find yesterday's date
by BrowserUk (Patriarch) on Nov 01, 2005 at 17:50 UTC
    Write down todays date and defer the problem until tomorrow.

    Originally posted as a Categorized Answer.