Here is one way:
use strict; use warnings; use DateTime; my $dt1 = DateTime->new ( year => 2013, month => 9, day => 29, ); my $dt2 = DateTime->new ( year => 2013, month => 3, day => 29, ); my $dt3 = DateTime->new ( year => 2013, month => 3, day => 30, ); my $dur1 = $dt2->subtract_datetime($dt1); printf "29/03/2013 to 29/09/2013: %s months\n", $dur1->months; my $dur2 = $dt3->subtract_datetime($dt1); printf "30/03/2013 to 29/09/2013: %s months\n", $dur2->months;
Output:
17:44 >perl 1919_SoPW.pl 29/03/2013 to 29/09/2013: 6 months 30/03/2013 to 29/09/2013: 5 months 17:44 >
Update: Please see the correction by haukex, below.
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
In reply to Re: More accurate way to calculate Date difference
by Athanasius
in thread More accurate way to calculate Date difference
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |