DateTime says:

If you only care about the date (calendar) portion of a datetime, you should use either delta_md() or delta_days(), not subtract_datetime().

In short, subtract_datetime gives you an answer that includes days, minutes, and seconds between the two times. But delta_days essentially chops off the "time" part of your DateTime object and then does the subtraction, so you only get days.

To see for yourself, try dumping the DateTime::Duration objects:

use DateTime; use DateTime::Format::ISO8601; my ($first, $last) = map { DateTime::Format::ISO8601->parse_dateti +me($_) } qw < 2013-08-01T20:10:31 2013-08-06T20:09:34 + >; my ($sub, $days) = map { $last->$_($first) } qw< subtract_datetime delta_days >; printf "%12s %5s | %-5s\n", 'Value', 'sub', 'days'; printf "%.21s+%.8s\n", ('-'x21)x2; for (qw< months days minutes seconds nanoseconds >) { printf "%12s : %5d | %-5d\n", $_, $sub->{$_}, $days->{$_}; }

Output:

Value sub | days ---------------------+-------- months : 0 | 0 days : 4 | 5 minutes : 1439 | 0 seconds : 3 | 0 nanoseconds : 0 | 0
use strict; use warnings; omitted for brevity.

In reply to Re: Inconsistent results of subtraction with DateTime? by rjt
in thread Inconsistent results of subtraction with DateTime? by jabowery

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.