in reply to Problem with DateTime subtract_datetime

Try
#!perl use strict; use DateTime; my $endDate = DateTime->new( year => 2015, month => 03, day => 24, time_zone => 'America/New_York' ); my $dt1 = DateTime->now( 'time_zone' => 'America/New_York' ); my $dur = $endDate->subtract_datetime($dt1); my ($m,$w,$d) = $dur->in_units( 'months','weeks','days' ); print "Expires in: $m months, $w weeks, $d days";
poj

Replies are listed 'Best First'.
Re^2: Problem with DateTime subtract_datetime
by flexvault (Monsignor) on Feb 19, 2015 at 16:25 UTC

    poj,

    Thanks for the sample.

    I modified your sample a little and was surprised with the results. See below: (Note: I have never used the 'in-units' before!)

    use strict; use DateTime; my $endDate = DateTime->new( year => 2015, month => 03, day => 30, time_zone => 'America/New_York' ); my $dt1 = DateTime->now( 'time_zone' => 'America/New_York' ); my $dur = $endDate->subtract_datetime($dt1); my ($m,$w,$d) = $dur->in_units( 'months','weeks','days' ); print "Expires in: $m months, $w weeks, $d days\n"; ($w,$d) = $dur->in_units( 'weeks','days' ); print "Expires in: $w weeks, $d days\n"; ($d) = $dur->in_units( 'days' ); print "Expires in: $d days\n"; __END__ Results: Expires in: 1 months, 1 weeks, 3 days Expires in: 1 weeks, 3 days Expires in: 10 days

    Could this be the source of the OP's discrepancies?

    Regards...Ed

    "Well done is better than well said." - Benjamin Franklin

      If so, the issue that months cannot be converted into days, since not all months have the same number of days. He would find $endDate->delta_days($dt1) more appropriate to his needs than $endDate->subtract_datetime($dt1)

        ikegami,

        I have read and re-read your comments several times, and I just don't understand how losing a month ( 28,29,30,31 days ) is not a problem. There is a finite number of days and weeks between the two dates.

        So, forget about the month(s), why are the weeks and days incorrect?

        Regards...Ed

        "Well done is better than well said." - Benjamin Franklin