Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Difference between two dates in years

by Doctrin (Beadle)
on May 14, 2013 at 14:38 UTC ( #1033486=perlquestion: print w/replies, xml ) Need Help??
Doctrin has asked for the wisdom of the Perl Monks concerning the following question:

Hello dear Monks! Is there a Perl module to get difference between two dates (in years), given as, say, '2012-03-22' and '1925-12-22' (notice that year is much earlier than 1970)? Thanks in advance. PS Would be great if this module could be installed on Win

Replies are listed 'Best First'.
Re: Difference between two dates in years
by ambrus (Abbot) on May 14, 2013 at 14:52 UTC

    Try Date::Manip.

    use Date::Manip; $dyear = Delta_Format(DateCalc('1925-12-22 00:00UTC', '2012-03-22 00:0 +0UTC'), '%yys'); # $dyear is now 86.249, the difference between the two dates in years
Re: Difference between two dates in years
by vagabonding electron (Chaplain) on May 14, 2013 at 16:41 UTC

    Time::Piece is a core module.

    #!/perl use strict; use warnings; use Time::Piece; my $datt = Time::Piece->strptime("2012-03-22", "%Y-%m-%d"); my $datf = Time::Piece->strptime("1925-12-22", "%Y-%m-%d"); my $diff = $datt - $datf; my $result = $diff->years; print "$result years\n"; $result = $diff->days; print "$result days\n";

    prints:

    C:\Perl\bin>perl pm_1033486.pl 86.2496055700018 years 31502 days

    See also Time::Seconds for the data values transformation.

Re: Difference between two dates in years
by hdb (Prior) on May 14, 2013 at 14:40 UTC

    Yes. (Warning: Mathematician's answer. Correct but useless.)

      So, if you're sure that there IS such a module (mathematicians cannot tell anything unproved?..), could you please tell me it's name?..

        Date::Calc should do, see function N_Delta_YMD.

Re: Difference between two dates in years
by Anonymous Monk on May 14, 2013 at 20:16 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2016-09-29 10:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:







    Results (551 votes). Check out past polls.