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

I want to check whether the parsing variable(which has date value) is 14 months older than current date or not?

Replies are listed 'Best First'.
Re: Date comparison
by choroba (Cardinal) on Aug 06, 2018 at 07:41 UTC
    Rather straightforward using DateTime. Note that "14 months" is a vague term, especially on days like February 29 or around midnight.

    #!/usr/bin/env perl use warnings; use strict; use feature qw{ say }; use DateTime; my $now = 'DateTime'->now; for my $day (6, 7) { my $dt = 'DateTime'->new(year => 2017, month => 6, day => $day); my $diff = $now - $dt; my ($years, $months) = ($diff->years, $diff->months); say $years == 1 && $months == 2 ? 'Yes' : 'No'; }

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      If you are willing to learn only one module for time related calculations, DateTime is your friend. It probably is always overkill, but I have never encountered the task it could not handle.

      UPDATE: Corrected module name.

      Bill
Re: Date comparison
by hippo (Archbishop) on Aug 06, 2018 at 08:00 UTC
Re: Date comparison
by thanos1983 (Parson) on Aug 06, 2018 at 08:08 UTC

    Hello Priya07,

    Welcome to the Monastery. Another possible way is to use the Date::Manip module:

    #!/usr/bin/perl use strict; use warnings; use Date::Manip; use feature 'say'; my $datestr = ParseDate("today"); say UnixDate($datestr,"Year:%Y Month: %b Day: %e"); my $deltastr = ParseDateDelta("14 months ago"); say UnixDate($deltastr,"Year:%Y Month: %b Day: %e"); say Date_Cmp($datestr, $deltastr); # gives you the difference say Date_Cmp($datestr, $datestr); # gives you no difference __END__ $ perl test.pl Year:2018 Month: Aug Day: 6 Year:2017 Month: Jun Day: 6 1 0

    Update: Removed one unnecessary line of code.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!