# timedate_diff

Definition: | include builtins\timedate.e atom a = timedate_diff(timedate td1, timedate td2, integer term=0)
Description: | Returns the difference in seconds between two dates. A positive value is returned when td1 is before td2. A term of DT_SECOND, DT_MINUTE, DT_HOUR, DT_DAY, DT_MONTH, or DT_YEAR may be supplied to truncate the difference to a whole number of that period (see example below). Note that DT_MONTH is treated as 30 days, and DT_YEAR uses 365.25 to calculate the whole number of years, but then multiplies by 365 to get back to seconds. If term is zero (the default) then no such rounding is attempted.
pwa/p2js: | Supported.
Comments: | As with timedelta, the result is intended to be used as a parameter to adjust_timedate(), and it can also be supplied as an argument to elapsed(). Note that results from date() should either have been generated with bMsecs:=true or have their [DT_MSEC] entries set to zero before being passed to timedate_diff().
Example: |
    include builtins\timedate.e
    timedate birthdate = parse_date_string("1/1/1960",{"D/M/YYYY"})
    printf(1,"You are %s old\n",{elapsed(timedate_diff(birthdate,date(),DT_DAY))})
    -- displays "You are 57 years, 304 days old"
By passing a whole number of days to elapsed(), the result contains no left-over hours, minutes, or seconds. Should you want months/weeks in the result, you would have to create a modified copy of builtins/pelapsed.e and obviously sort out messy details such as Jan 3rd to Feb 5th being 33 days or 1 month 2 days, as well as Feb 3rd to Mar 5th being 30 or 31 days yet (arguably) also 1 month 2 days, whether one or more leap years are pertinent to the equation or not - good luck!
See Also: | timedate, adjust_timedate, parse_date_string, elapsed