It's important to be clear on definitions and provide meaningful examples. For example, sometimes a "month" is defined as exactly 30 days, but since you're asking this question, I'm guessing that's not what you want. For example, using DateTime math:

2016-01-29 + 30 days = 2016-02-28    2017-01-29 + 30 days = 2017-02-28
2016-01-30 + 30 days = 2016-02-29    2017-01-30 + 30 days = 2017-03-01
vs.
2016-01-29 + 1 month = 2016-02-29    2017-01-29 + 1 month = 2017-03-01
2016-01-30 + 1 month = 2016-03-01    2017-01-30 + 1 month = 2017-03-02

I would recommend this: First, calculate the "deadline" date by adding your "6 months" to the $date_birth. Then, use the methods provided by the library to see if the $diab_confirm is past that date or not. For example, in Date::Calc that would probably be Add_Delta_YM for the first part, and Date_to_Days or Delta_Days for the second.

Personally, I like DateTime, and its objects overload the comparison operators:

use warnings; use strict; use DateTime; use DateTime::Format::Strptime; my $date_birth = '21/02/2013'; my $diab_confirm = '29/08/2013'; my $strp = DateTime::Format::Strptime->new(on_error=>'croak', pattern => '%d/%m/%Y'); my $dt_birth = $strp->parse_datetime($date_birth); my $dt_confirm = $strp->parse_datetime($diab_confirm); print " birth: ", $dt_birth->ymd, "\n"; print " confirm: ", $dt_confirm->ymd, "\n"; my $dt_deadline = $dt_birth->clone->add(months=>6); print "deadline: ", $dt_deadline->ymd, "\n"; if ($dt_confirm > $dt_deadline) { print $dt_confirm->ymd, " > ", $dt_deadline->ymd, "\n"; } else { print $dt_confirm->ymd, " <= ", $dt_deadline->ymd, "\n"; } __END__ birth: 2013-02-21 confirm: 2013-08-29 deadline: 2013-08-21 2013-08-29 > 2013-08-21

As always, the more test cases the better!


In reply to Re: More accurate way to calculate Date difference by haukex
in thread More accurate way to calculate Date difference by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.