subject: Undocumented Microsoft Excel Functions - the MS Excel DateDif Function [print this page] Undocumented Microsoft Excel Functions - the MS Excel DateDif Function
Users of Access may know that it supports a useful function called DateDiff, which returns the difference between two dates expressed in units chosen by you, the user of Excel. Want to know how many months it is to Christmas? How many days it is since your birthday? The DateDif function will tell you - but reluctantly.
The syntax of the function is:
=DateDif(First date, Second date, Code)
where:
"First date" is the earlier of the two dates
"Second date" is the later of the two dates
"Code" determines how you want to calculate the difference. It can take 6 possible values: "d", "m", "y", "ym", "yd" and "md". To see what these mean, consider the example where:
- the first date is 4th March 1964
- the second date is 29th November 2010
Then here's what the DATEDIF function would return for each of the 6 possible codes:
- "d" would return 17071 (the number of days between the two dates0
- "m" would return 560 (the number of months between the two dates)
- "y" would return 46 (the number of years between the two dates)
- "ym" would return 8 (the number of months between the two dates if they were both in the same year)
- "yd" would return 270 (the number of days between the two dates, if they were both in the same year)
- "md" would return 25 (the number of days between the two dates, if they were both in the same month and year
Note that the Excel DateDif function doesn't support arguments like "q" (for quarters), unlike its Access equivalent.