laziness, impatience, and hubris PerlMonks

### Difference between two dates in years

 on May 14, 2013 at 14:38 UTC 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

Re: Difference between two dates in years
by hdb (Monsignor) 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

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1033486]
Approved by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2017-08-22 04:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Who is your favorite scientist and why?

Results (328 votes). Check out past polls.

Notices?