There are modules that will do this for you. I acknowledge you indicate you do not wish to use a module.

So the first step is to figure out the algorithm you wish to use. Grab pencil and paper (or a whiteboard for you youngins), and start trying to figure out how you would do the math by hand.

Then try to write code that would do the same thing you did by hand.

The number of things you are likely to overlook or get wrong are controllable in this example, but high enough to reward your use of a Module where someone else (or several someone elses) took the time to iron out the bugs for you.

But if you just gotta write the code yourself, Step 1 is to figure out how to do it by hand.

P.S. I would suggest researching Leap Year Rules. Assuming your needs do not extend to earlier than Friday, October 15, 1582 in Catholic countries, 1698 or 1752 in principalities headed by Protestants, or 1918 in Russia, a shortcut to use only the modern computation should suffice.

In that computation, the base assumption is that every year has 365 days unless it is declared to be a leap year, in which case it has 366.

More specifically, February would have 29 days rather than the hard-coded 28 you presume in your code.

Research the Leap Year Rules, and you should be able to devise a subroutine which can determine if any given year is a leap year.

From there, you should have all the tools you need to compute number of days between dates, given sufficient forethought to the construction of either your computational algorithms, or your foreach loops -- or some combination thereof.

Update: Okay, fine, here's one example of the many things a module could do for you that you would have to grind through to do by hand:

sub isLeapyear { my ($year, @extraStuff) = @_; if (!defined $year) { $year = 0; } my $leapYearFlag = 0; # Assume not a leap year until proven + otherwise. if ($year % 4) { # Not divisible by 4. Cannot be a leap year. $leapYearFlag = 0; } else { # Divisible by 4. Possibly a leap year. if ($year % 100) { # Is not divisible by 100. It is a leap year. $leapYearFlag = 1; } else { # Is divisible by 100. May not be a leap year. if ($year % 400) { # Is not divisible by 400. Cannot be a leap year. $leapYearFlag = 0; } else { # Is divisible by 400. IS a leap year. $leapYearFlag = 1; } } } return $leapYearFlag; }

In reply to Re: perl basic count days between two dates by marinersk
in thread perl basic count days between two dates by scripter87

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.