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

Another date related one.....

by meetn2veg (Scribe)
on Oct 11, 2002 at 13:13 UTC ( #204588=perlquestion: print w/replies, xml ) Need Help??

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

Right! Looking to find the difference in days between 2 dates in the future (stored in dd/mm/yy format - but makes no odds:split / etc...).

I've gone through the Q&A here, I've had a look at Date::Calc et al (made things worse!) and I'm really getting my knickers in a twist!

Do I really need to convert dd/mm/yy into seconds? Dates overlapping months? Dates overlapping years? What about Leap years?? AARRGGHH!!

Still relatively new etc.. but with Date::Calc, how do you pass the respective vars to it? All I'm after is $date_in, $date_out and $date_diff. Is that too much to ask?!?

Eagerly awaiting any help (and probably pressing F5 every couple of seconds!!!).


Replies are listed 'Best First'.
Re: Another date related one.....
by lestrrat (Deacon) on Oct 11, 2002 at 13:25 UTC

    Uh, what's the problem with Date::Calc's Delta_Days()?

    use Date::Calc qw/ Delta_Days /; my $date1 = "01/12/2003"; my $date2 = "01/01/2004"; my( $d1, $m1, $y1 ) = split( /\//, $date1 ); my( $d2, $m2, $y2 ) = split( /\//, $date2 ); my $dd = Delta_Days( $y1, $m1, $d1, $y2, $m2, $d2 ); print "diff is $dd days\n";

    (note: I noticed you had dd/mm/yy, but I got lazy ;)

Re: Another date related one.....
by Zaxo (Archbishop) on Oct 11, 2002 at 13:25 UTC

    Take another look at Date::Calc.

    use Date::Calc qw( Delta_YMD ); my ($date_first, $date_next) = ("25/8/2003","1/12/2004"); my $diff = Delta_YMD(reverse( split "/", $date_first), reverse( split "/", $date_next));

    After Compline,

Re: Another date related one.....
by bigj (Monk) on Oct 11, 2002 at 13:33 UTC
    The Date::Calc module provides the Delta_Days method. It should be possible to calculate the difference between two days. E.g.:
    use Date::Calc qw/Delta_Days/; my $date1 = '27/01/03'; my $date2 = '01/02/03'; my $delta = Delta_Days(map {reverse split m:/:} ($date1, $date2)); print $delta;
Re: Another date related one.....
by RMGir (Prior) on Oct 11, 2002 at 13:54 UTC
    As always, when everyone suggests Date::Calc (and really, it DOES do what you need), I'll suggest Date::Manip.

    D::M is slower than D::C, but it supports things like next business day, if you need that. If you don't, stick with D::C and Add_Delta_Days.

Re: Another date related one.....
by blakem (Monsignor) on Oct 11, 2002 at 17:58 UTC
    I prefer Time::Piece.
    #!/usr/bin/perl -wT use strict; use Time::Piece; my $date1 = "01/01/2004"; my $date2 = "01/12/2003"; my $format = "%d/%m/%Y"; my $diff = Time::Piece->strptime($date1, $format) - Time::Piece->strptime($date2, $format); print $diff->days . " Days\n"; __END__ 31 Days


Re: Another date related one.....
by meetn2veg (Scribe) on Oct 11, 2002 at 15:50 UTC
    Ahh! "I see" said the blind man, as he picked up his hammer and saw.

    Thank you so much (the 4 above this). I had seen the Delta_Days in CPAN listings but bypassed it 'cos I didn't know what is did!

    Rest assured I'll go away and have a fiddle now things are a bit clearer to me.

    Many thanks again to you all.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2020-10-31 06:57 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (287 votes). Check out past polls.