-1

how to get year difference between two date in perl like below date

my $date1 = '2012-08-29T00:00:00.0000Z'
my $date2 = '2010-08-29T00:00:00.0000Z'

need help and advise on it .thanks

Thiem Nguyen
  • 6,345
  • 7
  • 30
  • 50
Zann
  • 85
  • 1
  • 2
  • 8
  • 1
    *Very* similar: http://stackoverflow.com/questions/821423/how-can-i-calculate-the-number-of-days-between-two-dates-in-perl – Thilo Jun 19 '12 at 02:30

3 Answers3

9
use DateTime::Format::RFC3339 qw( );
my $format = DateTime::Format::RFC3339->new();

my $dt1 = $format->parse_datetime('2010-08-29T00:00:00.0000Z');
my $dt2 = $format->parse_datetime('2012-08-29T00:00:00.0000Z');
my $dur = $dt2 - $dt1;

say sprintf "%d years", $dur->in_units('years');

Ref: DateTime, DateTime::Duration, DateTime::Format::RFC3339

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • between i facing this error when using above code Undefined subroutine &Params::Validate::SCALAR called at /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi/DateTime/Locale.pm line 41. – Zann Jun 19 '12 at 02:54
  • 3
    @Zann, we've previously established that you mangled your installation of DateTime::Locale and need to reinstall it. – ikegami Jun 19 '12 at 03:27
  • have been reinstalled.but still getting some error. cpan DateTime::Locale CPAN: Storable loaded ok Going to read /root/.cpan/Metadata Database was generated on Tue, 19 Jun 2012 01:16:49 GMT DateTime::Locale is up to date. [root@sgnic2 ~]# cd /home/websites/sgnic/cgi-bin [root@sgnic2 cgi-bin]# perl zann.pl Undefined subroutine &Params::Validate::SCALAR called at /usr/lib/perl5/site_per l/5.8.8/i386-linux-thread-multi/DateTime/Locale.pm line 41. – Zann Jun 19 '12 at 05:35
  • 2
    This is not the place to debug your broken installations. Create a new question so that you can give proper information and show what you've done to debug it. – ikegami Jun 19 '12 at 05:46
1

A lightweight alternative to the correct solutions others will post:

my $date1 = '2012-08-29T00:00:00.0000Z';
my $date2 = '2010-08-29T00:00:00.0000Z';
my $years_diff = do { no warnings "numeric"; $date1 - $date2 };

Note that there are probably dozens of different ways to specify how you would want partial years to be rounded in this seemingly trivial question.

ysth
  • 96,171
  • 6
  • 121
  • 214
  • 1
    That doesn't give the right answer for most inputs. e.g. change `$date1` to `2012-08-28T00:00:00.0000Z`. – ikegami Jun 19 '12 at 03:27
  • How do you know what answer is wanted in that case, ikegami? As I said, there are many different ways to round. – ysth Jun 19 '12 at 05:56
  • Aside that the OP didn't ask to round, your code doesn't round. It sometimes returns 2 and sometimes returns 1 for a duration of 2 years minus one day. (You gotta tag me with @ikegami if I'm neither the OP nor the Answer's author. It's a fluke I noticed your question.) – ikegami Jun 19 '12 at 15:36
  • @ikegami my code returns year(date1) - year(date2). That is just as likely an interpretation of "how to get year difference between two date" as your year(duration of date1-date2) – ysth Jun 19 '12 at 18:03
0

If you don't care about hours or leap years, you can truncate off the times and use Date::Simple.

my $days = Date::Simple->new(substr $date2, 0, 10) - Date::Simple->new(substr $date1, 0, 10);
my $years = int($days / 365);
Dondi Michael Stroma
  • 4,668
  • 18
  • 21