DATEDIF: Finding the Difference Between Two Dates

How to:

The DATEDIF function returns the difference between two full component standard dates in desired components.

A component is one of the following:

DATEDIF returns a whole number. If the difference between two dates is not a whole number, DATEDIF truncates the value to the next largest integer. For example, the number of years between March 2, 2001, and March 1, 2002, is zero. If the end date is before the start date, DATEDIF returns a negative number.


Top of page

x
Syntax: How to Find the Difference Between Two Dates
DATEDIF('from_date', 'to_date', 'component_code')

where:

from_date

Date. Is the start date from which to calculate the difference.

to_date

Date.Is the end date from which to calculate the difference.

component_code

Alphanumeric. Is one of the following enclosed in single quotation marks:

Y indicates a year component.
'M' indicates a month component.
'D' indicates a day component.
'WD' indicates a weekday component.
'BD' indicates a business day component.

Note: DATEDIF does not use output_format because for the result it uses the format 'I8'.



Example: Finding the Difference Between Two Dates

The example finds the number of complete months between today, March 23, 2004, and one specific day in the past

DATEDIF('September 11 2001', '20040323', 'M')

and returns 30, which can be assigned to a numeric field.

Tip: There is an alternative way to find the difference between dates. As long as any standard date is presented internally as a whole number of the least significant component units (that is, a number of days for full component dates, a number of months for YYM or MY format dates, etc.), you can find the difference in these component units (not any units) directly, without DATEDIF. For example, assume OLD_YYM_DT is a date field in format MYY and NEW_YYM_DT is another date in format YYM. Note that the least significant component for both formats is month, M. The difference in months, then, can be found by subtracting the field OLD_YYM_DT from NEW_YYM_DT in the following statement:

MYDIFF/I8 = NEW_YYM_DT/YYM - OLD_YYM_DT;

Otherwise, non-full component standard dates or legacy dates should be converted to full component standard dates before using DATEDIF.


iWay Software