jflevi has asked for the wisdom of the Perl Monks concerning the following question:

No, it is not a dating question !!!
Is there a function that will return the numbers of days between two dates (from 1900 to present)
I looked at "perldoc", didn't see anything I liked ... Saw also the DateTime module - fell asleep halfway through ;-) actually it is not in my Llama league ...

Thanks

Life is tough, it's tougher when you are dumb...

jfl

Replies are listed 'Best First'.
Re: Days between 2 dates ?
by anthski (Scribe) on Feb 26, 2008 at 03:40 UTC
    I'm not sure if you want to simply know the number of days between two given dates, or all of the dates between the two given dates, but Date::Simple and Date::Range can do what you want. i.e
    #!/usr/bin/perl -w use strict; use Date::Simple; use Date::Range; # Define our dates my $date1 = Date::Simple->new('2006-02-02'); my $date2 = Date::Simple->new('2007-02-02'); # Create our range my $range = Date::Range->new($date1, $date2); # Find the number of days between the specified dates my $number_of_days = $range->length; print "Number of days between dates: $number_of_days\n"; # Now print each of these dates foreach my $date ($range->dates) { print $date->as_str . "\n"; }
      Thanks a lot for pointing me to this module; it was a breeze to install, and with
      use Date::Simple ('date');
      I just needed:
      $diff = date($enddate) - date($startdate);
      I had my question solved, simple and easy.

      You Monks ROCK !!!

      Life is tough, it's tougher when you are dumb...

      jfl

Re: Days between 2 dates ?
by kyle (Abbot) on Feb 26, 2008 at 03:42 UTC
Re: Days between 2 dates ?
by mhearse (Chaplain) on Feb 26, 2008 at 03:46 UTC
    Handling dates can be messy. Have a look at Date::Calc. It contains a Delta_Days function which may meet your needs.
Re: Days between 2 dates ?
by bcrowell2 (Friar) on Feb 26, 2008 at 03:52 UTC
    If you really just want a snippet of code, rather than something complex from CPAN, you could take a look here: http://www.lightandmatter.com/when/when.html . See package When;, and sub delta_days(). But in general you're probably better off just grabbing Date::Calc off of CPAN.
Re: Days between 2 dates ?
by Anonymous Monk on Feb 26, 2008 at 08:10 UTC
    Check out Time::Duration
    use Time::Duration; print "Runtime ", duration(time() - $start_time), ".\n";
    If the time() - $start_time is 3820 seconds (which is exactly 1h, 3m, 40s), you get it rounded to fit within two expressed units: "Runtime: 1 hour and 4 minutes.". Using duration_exact instead would return "Runtime: 1 hour, 3 minutes, and 40 seconds".