simple math is all you need.

Except that the mathematics of date-handling is rarely simple. In this case you've made the error of assuming there are 24 hours in the day.

In the UK, for example, the clocks have just moved forwards by an hour for British Summer Time: Sunday only had 23 hours in it.

Most of the time your code will work just fine. But if it happens to be run in the first hour of a day then it will yield the wrong output, skipping a day.

For example, if I ran it at 00:17 early tomorrow (Wednesday) morning with $NUM_DAYS set to 2 then it would correctly output:

Mon Mar 28 00:17:00 2005

But setting $NUM_DAYS to 3 would yield:

Sat Mar 26 23:17:00 2005

The original poster just wanted dates; if you truncate the time portions off the above then you've ended up skipping a day, jumping straight from Monday to Saturday!

Now it would be possible to fix this, but it isn't worth trying when so many hours of hard thinking have already been put into getting these sorts of things write by the creators of the DateTime module and friends.

Do not try to do arithmetic with dates. You will get it wrong.

Smylers


In reply to Re^3: Getting previous dates from the current date by Smylers
in thread Getting previous dates from the current date by TASdvlper

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.