Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comparing two dates and getting back difference in days

by TASdvlper (Monk)
on Jan 21, 2004 at 16:28 UTC ( #322933=perlquestion: print w/ replies, xml ) Need Help??
TASdvlper has asked for the wisdom of the Perl Monks concerning the following question:

Hello all, I have two dates, in the format YYYY-MM-DD, say ...
use Date::Manip; my $date1 = "2004-01-14" my $date2 = UnixDate(ParseDate("today"),"%Y-%m-%d");
I'm trying to use Date_Cmp($date1, $date2) to get the difference in days (I don't care about hours, minutes, seconds, ..) but I keep getting back "1" as the result. Am I doing something wrong ?

I've also read that Date::Manip is a very big and slow module. Is there a better one to use for this type of calculation ?

As always, thanks in advance.

Comment on Comparing two dates and getting back difference in days
Download Code
Re: Comparing two dates and getting back difference in days
by Roy Johnson (Monsignor) on Jan 21, 2004 at 16:34 UTC
    How about the Delta_Days function in Date::Calc? Date_Cmp is just like cmp, returning +1, 0, or -1 to indicate greater than, equal, or less than.

    The PerlMonk tr/// Advocate
Re: Comparing two dates and getting back difference in days
by jdtoronto (Prior) on Jan 21, 2004 at 16:35 UTC
    Try Time::Piece. I discovered it a few days back and it is mucho-small, very fast and does the basic comparisons. It has relatives Time::Piece::MySQL for MySQL date/time types and when you do a calculation it returns a Time::Seconds object. Very nice little bundle.

    I also use Date::Manip, but it is too big and heavy for this sort of lifting!

    jdtoronto

Re: Comparing two dates and getting back difference in days
by duff (Vicar) on Jan 21, 2004 at 16:38 UTC

    There's always good old Time::Local and hand parsing (untested):

    #!/usr/bin/perl use Time::Local; my $now_gmt = time; my $then = "2004-01-14"; my ($ty,$tm,$td) = $then =~ /^(\d\d\d\d)-(\d\d)-(\d\d)$/; my $then_gmt = timegm(0,0,0,$td,$tm-1,$ty); my $diff_in_days = int(($now_gmt - $then_gmt) / 86400);
Re: Comparing two dates and getting back difference in days
by Aragorn (Curate) on Jan 21, 2004 at 16:38 UTC
    Date_Cmp just returns if $date1 is smaller than, equal to, or greater than $date2, like the Perl cmp operator.

    What you want to do can be done with the DateCalc function. See the documentation of Date::Manip for details.

    The Date::Calc module is implemented mostly in C and should be a lot faster.

    Arjen

      Ok, I'm looking at DateCalc but I can't seem to figure out how to get the output in just days. I'm getting output that looks like this:
      2004-01-14 2004-01-21 +0:0:1:0:0:0:0
      Do I have to do my own conversion to just days ? Also, I could seem to figure out what the $err was supposed to be.
Re: Comparing two dates and getting back difference in days
by Limbic~Region (Chancellor) on Jan 21, 2004 at 17:01 UTC
    TASdvlper,
    Here is a TIMTOWTDI solution. The Seconds2English module you will not find on CPAN as I wrote for learning purposes. I have actually refined it quite a bit, but unfortunately that code is unavailable at the moment.
    #!/usr/bin/perl -w use strict; use Time::Local; use Seconds2English; my $date1 = epoch('2004-01-14'); my $date2 = epoch('2003-12-25'); my $diff = Seconds2English->new('start' => $date2 - $date1); print "The difference is " , $diff->in_days, " days\n"; sub epoch { my ($year, $month, $day) = split '-' , shift; return timelocal(0 , 0 , 12, $day , $month - 1, $year); } __END__ The difference is 20 days
    Cheers - L~R
Re: Comparing two dates and getting back difference in days
by Anonymous Monk on Jan 21, 2004 at 17:23 UTC
    Date::Simple does a nice job for simple calculations.

    Chris

    #!/usr/bin/perl use strict; use warnings; use Date::Simple; my $date1 = Date::Simple->new("2004-01-14"); my $date2 = Date::Simple->new("2004-01-17"); print "Days difference is: ", $date2 - $date1, "\n";
Re: Comparing two dates and getting back difference in days
by flyingmoose (Priest) on Jan 21, 2004 at 17:24 UTC
    So many Date and Time modules to try, so little time :)
      That's how it used to be before DateTime. That's why there is only DateTime now.
      use DateTime; use strict; use warnings; my $str = "2004-01-14"; my( $y, $m, $d ) = split /\D/,$str; my $delta = DateTime->new( year => $y, month => $m, day => $d, )->delta_days( DateTime->now(), ); die $delta->days; __END__ 0 at datetime4.pl line 16.
        DateTime homepage

        Is there an ActiveState repository with it?


        The PerlMonk tr/// Advocate
Re: Comparing two dates and getting back difference in days
by phydeauxarff (Priest) on Jan 21, 2004 at 17:37 UTC
    Throwing in one more thing to consider when comparing dates...

    I use date::business because I typically need to figure out how many "business days" have passed and this module does a good job of not only figuring the weekdays, but also allows you to skip holidays.

    using the diffb() function in this module will count the number of days between a Friday and a Monday as 1.

Re: Comparing two dates and getting back difference in days
by BrowserUk (Pope) on Jan 21, 2004 at 18:23 UTC

      If you look at the rest of the nodes that I have written you will discover that I am a big Date::Manip fan.

      That been said, I think that "slow" is a relative term. Yes it is big, but when ever I have implimented it into my scripts, I have found very little, if any, time lag. If you are trying to scrape every last clock cycle out of a huge script, you probably don't want to use it. But if a milli-second or so doesn't matter to you, the flexibility of Date::Manip makes it more than worth it. I can honestly say that I haven't used any of the other Date conversion modules, mainly because Date::Manip has everything that I need. I guess once you get to know a module, you kind of get stuck on it. Mind you that I am not knocking any of the other modules, I just think that for what you get out of it, Date::Manip is where it's at.

      Paulster2

Re: Comparing two dates and getting back difference in days
by injunjoel (Priest) on Jan 22, 2004 at 18:30 UTC
    Greetings all,
    A little late I know but here is what I use for small cases.
    #!/usr/bin/perl -w use strict; use Time::Local; my $start_date_str = "2004-01-14";#date you are testing; my ($year,$month,$day) = split(/\-/,$start_date_str); $month--;#timelocal months are 0-11; my $start_seconds = timelocal(0,0,0,$day, $month, $year); my $today_seconds = time(); my $days_between = ( int( ( ( (abs($start_seconds - $today_seconds))/6 +0 )/60 )/24 ) ); print $days_between; exit;
    Previewing it I must admit its not very elegant, but it works for me.
    Hope that helps get you part of the way there.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://322933]
Approved by bassplayer
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2014-08-01 23:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (51 votes), past polls