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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |