You are on page 1of 3

The DATEDIF Function

The DATEDIF function is a worksheet function that computes the difference between two dates. This function is available in all versions of Excel since version 5, but is documented onl in the help files for Excel !""". It isn#t documented in either Excel $% or Excel !""!. DATEDIF has, for whatever reason, been treated as one of the drunk cousins of the Function Famil . Excel knows he lives a happ and useful existence, and will acknowled&e his existence when ou ask, but will never mention him in 'polite' conversation. (ou can use DATEDIF as ou would an normal worksheet function, because it is a normal worksheet function. )ntil Excel!""", it was never documented in the help files, but it has been around since at least Excel5. There has been, from time to time, an article in the *nowled&e +ase describin& DATEDIF, but as often as not, the article is not available. ,h - ,ho knows- .ne of the deep dark secrets known onl the /ofties. The s ntax for DATEDIF is as follows0 =DATEDIF(Date1,Date2,Interval) ,here Date1 is the first date, in standard Excel serial1date format. Date2 is the second date, in standard Excel serial1date format. Interval indicates the unit of time that =DATEDIF is to return is result. Date1 must be less than 2earlier3 or e4ual to Date2. .therwise, =DATEDIF will return a #NUM! error. If either Date1 or Date2 is not a date, =DATEDIF will return a #VALUE! error. Interval must be one of the followin& codes0 Code "m" "d "y" "ym" "yd" Meaning 5onths Days (ears Description The number of complete months between Date6 and Date!. The number of da s between Date6 and Date!. The number of complete ears between Date6 and Date!.

5onths Excludin& The number of months between Date6 and Date!, (ear as if Date6 and Date! were in the same ear. Da s Excludin& (ears The number of da s between Date6 and Date!, as if Date6 and Date! were in the same ear.

The number of da s between Date6 and Date!, as Da s Excludin& "md" if Date6 and Date! were in the same month and the 5onths And (ears same ear.

When passing the interval code to the DATEDIF function, enclose it in quotes if you are passing a literal value to the function:
=DATEDIF(A1,NOW(),"d")

However, if your interval code is stored in a worksheet cell, it should not be

enclosed in quotes in the cell.


The table on the ri&ht displa s the results of the various interval codes for two dates. 7a attention to the result returned b the ym interval code. The 5 indicates that there are 5 calendar months between 8anuar and 8une 11 the ears are i&nored. The same is true with the md interval. There are 69 calendar da s between the two dates when the months and ears are i&nored.

:alculatin& A 7erson#s A&e


A fre4uent use of the DATEDIF is to compute someone#s a&e based on the current date and their birthda . The formula below will return someone#s exact a&e based on their birthda in cell A6. =DATEDIF(A1,NOW(),"y") & " years, " & DATEDIF(A1,NOW(),"ym") & " months, " & DATEDIF(A1,NOW(),"md") & " days" This will return a strin& like 33 years, m!nt"s, 1# days

(ou can#t use DATEDIF in ;+A code. ;+A provides a function called DateDiff 2note, two f#s3, but DateDiff doesn#t support the ' m', 'md', and ' d' interval ar&uments that DATEDIF does. To compute a&e in ;+A, ou have to do the math on our own. F$n%t&!n A'e(Date1 As Date, Date2 As Date) As *tr&n' D&m + As Inte'er D&m M As Inte'er D&m D As Inte'er D&m Tem,1 As Date Tem,1 = Date*er&al(+ear(Date2), M!nt"(Date1), Day(Date1)) + = +ear(Date2) - +ear(Date1) . (Tem,1 / Date2) M = M!nt"(Date2) - M!nt"(Date1) - (12 0 (Tem,1 / Date2)) D = Day(Date2) - Day(Date1) I1 D 2 3 T"en M = M - 1 D = Day(Date*er&al(+ear(date2), M!nt"(date2), 3)) .

D End I1 A'e = + 4 " years " 4 M 4 " m!nt"s " 4 D 4 " days" End F$n%t&!n

DATEDIF And <eap (ears


,hen ou are usin& the yd interval, Excel will use the ear of the first date, Date6, to determine whether the to include !$1Februar in the calculation. In other words, it uses the ear of Date6 as the ear for both dates. For example, if Date6 is 61Feb16$$=, and Date! is 615ar16$$%, =DATEDIF(Date1,Date2,"yd") will return !$, because there are !$ da s between 61Feb16$$= and 615ar16$$=. If Date6 is chan&ed to 61Feb16$$% and Date! remains 615ar16$$%, the same function will return !>, the number of da s between 61Feb16$$% and 615ar16$$%. /imilarl , if Date6 is 61Feb16$$5 and Date! is 615ar16$$=, the result is !> because both dates are &iven the ear of the first date, which, in these last two examples, is not a leap ear.

You might also like