• ...perl basic..."
  • ...I am very new to perl...
  • ...and programming in general.
  • ...question about getting the amount of days between two dates.
  • ...any advice for me.?

We were all new to Perl at some point, and all new to programming in general. Nothing wrong with that. But this is not a "basic" problem. However, Perl does provide tools to help you avoid becoming an expert in date calculations. My advice: If you're not using the power of Perl's modules (core and CPAN), you're not programming Perl. You're just using its syntax and interpreter.

Your technique is neglecting leap years, isn't it?

Here is a CPANy version:

use strict; use warnings; use DateTime; use IO::Prompt::Tiny qw( prompt ); # Get a DateTime object for birthday and now based on user input. my( $bday, $now ) = map { DateTime->new( prompt_date( $_ ) ) } qw( Birth Today's ); die "Birth date must be in the past.\n" unless $bday < $now; # Perform the math. print "\nYou are ", $bday->delta_days($now)->in_units('days'), " days +old.\n"; # Prompting: pass date entry type, receive a day=>__, month=>__, year= +>__ hash. sub prompt_date { print shift, " date entry:\n"; return map { $_ => prompt "\tPlease enter $_:" } qw( day month year +); }

A sample run:

Birth date entry: Please enter day: 27 Please enter month: 9 Please enter year: 1954 Today's date entry: Please enter day: 02 Please enter month: 10 Please enter year: 2013 You are 21555 days old.

(I used the birth date of a famous individual in the Perl community.)

Date calculations are very hard to get right. At minimum we have to deal with leap years. There could be other challenges that I'm not aware of, and since I don't want to become an expert in nuances of the Gregorian Calendar, I offload as much of that burden as possible to the DateTime module.

Prompting with a message isn't all that hard, but IO::Prompt::Tiny seems more convenient than a bunch of print statements, <STDIN> invocations, and chomp, so I used that module too.

Once we've handled the prompting and the date manipulation, there's not much left to do except a quick sanity check, and printing the result of the calculation. Keep in mind that there are countless problem domains in the universe. As a programmer you will be asked to become an expert on some of them. But it would be impossible to become an expert on all of them. Using well-tested modules that solve problems such that you don't need to become an expert on a peripheral topic will enable you to focus more on becoming a great programmer, and less on becoming a great date calculator, genome researcher, and so on. There's no shame in using available tools to make your life as a programmer easier. In fact, it's smart; you become more effective, and your code becomes more robust.

A little more terse:

use strict; use warnings; use DateTime; use IO::Prompt::Tiny qw( prompt ); # Get a DateTime object for birthday and now based on user input. my( $bday, $now ) = map { print "$_ date entry:\n"; DateTime->new( map { $_ => prompt "\tPlease enter $_:" } qw(day mont +h year) ) } qw(Birth Today's); die "Birth date must be in the past.\n" unless $bday < $now; # Perform the math. print "\nYou are ", $bday->delta_days($now)->in_units('days'), " days +old.\n";

Dave


In reply to Re: perl basic count days between two dates by davido
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.