Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How do i find yesterday's date

( #88053=categorized question: print w/ replies, xml ) Need Help??
Contributed by ganesh_saravanan on Jun 13, 2001 at 17:36 UTC
Q&A  > dates and times


Answer: How do i find yesterday's date
contributed by footpad

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

Answer: How do i find yesterday's date
contributed by marcink

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
Answer: How do i find yesterday's date
contributed by monsterzero

You can use DateTime for this

use DateTime; my $dt = DateTime->now()->subtract( days => 1 );
Answer: How do i find yesterday's date
contributed by Pedro Picasso

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.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (4)
    As of 2015-07-04 02:57 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (57 votes), past polls