As brian_d_foy said, "yes". There are numerous ways to do it.

Basically, find out today's date, and subtract 1 day. If this results in a day number of 0, subtract 1 from the month, and reset the day to the last day yesterday's month. If the new month is 0 (assuming January is 1), subract 1 day from the year, and set the month to December.

In principle, this is no different than any other sort of arithmetic involving borrowing and carrying.

So, you need one array: the number of days in each month (remembering that February sometimes has 29, not 28, days), the logic for determining leap year, (hint: 1900 was not a leap year), and the logic for when month and year boundaries are crossed.

Or you could use something like this, in the spirit of Corion's response):

$today = localtime; sleep(-86400); $yesterday = localtime; print "Yesterday's date was $yesterday\n";

This will use less wall clock time than Corion's solution, but it requires quite a lot of CPU time, or access to a naked singularity. Even so, sleeping for negative time periods is supported on very few O/S; you may need to install the Acme::Time::Reversal module from CPAN.

<

emc

At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

—Igor Sikorsky, reported in AOPA Pilot magazine February 2003.

In reply to Re: Date Manipulation by swampyankee
in thread Date Manipulation by SR

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, 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.