Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Do a DATEDIF in Excel: Step-by-Step Guide

· 7 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To calculate the difference between two dates in Excel, type =DATEDIF(start_date, end_date, "unit") into a cell, where start_date and end_date are cell references or dates and unit is a code such as "d" for days, "m" for whole months or "y" for whole years. For example, =DATEDIF(A2, B2, "y") returns the number of complete years between the dates in A2 and B2. That single formula covers most date-difference questions, including age calculations, but it has quirks worth knowing before you rely on it.

DATEDIF is a hidden function: it works in every current version of Excel, but it does not appear in the formula autocomplete list, so you have to type the whole name yourself. It also produces whole numbers only — it counts complete units, not fractions — which is exactly what you want for ages and anniversaries and exactly what you do not want if you need years to two decimal places.

What You'll Need

  • Excel on Windows, Mac, or Excel for the web (DATEDIF behaves the same on all of them).
  • Two valid dates. Genuine Excel dates, not text that merely looks like a date.
  • A cell for the result, with the format set to General or Number (not Date), or the answer will display as a date serial number instead of a count.

Step-by-Step

  1. Make sure the dates are real dates. A real Excel date is a serial number formatted to look like a date. Type a date with slashes or hyphens (for example 15/03/2024 or 3/15/2024) and press Enter. If Excel recognises it, the value will align to the right by default and change format when you apply a date format. If it stays on the left, Excel has stored it as text and DATEDIF will return an error.
  2. Check your regional date order. In a locale that uses day/month/year, typing 03/04/2024 means 3 April; in month/day/year locales it means 4 March. If dates were imported or pasted from another system, the day and month may be swapped. Verify a few entries against the source before calculating anything.
  3. Set up three columns: start date, end date, result. Label A1 as Start, B1 as End, and C1 as Difference. Enter the earlier date in A2 and the later date in B2. DATEDIF requires the start date to be earlier than or equal to the end date.
  4. Type the formula. In C2 enter =DATEDIF(A2,B2,"d") for total days, =DATEDIF(A2,B2,"m") for complete months, or =DATEDIF(A2,B2,"y") for complete years. Press Enter. Do not try to pick the function from the autocomplete dropdown — it is not listed, and typing "DAT" will suggest DATE and DATEVALUE instead.
  5. Choose the unit that matches the question. The supported codes are "d" (days), "m" (months), "y" (years), "ym" (months ignoring years), "yd" (days ignoring years) and "md" (days ignoring months and years). Use "y" and "ym" together to build an "X years, Y months" label, and "d" alone when you need the raw day count.
  6. Combine codes for a readable result. In the next cell enter =DATEDIF(A2,B2,"y")&" years, "&DATEDIF(A2,B2,"ym")&" months". The ampersands join text and numbers into one string. This is the standard way to produce an age or a length of service.
  7. Format the result cell. Select column C, then set the number format to General or Number with zero decimal places. If a date format was copied down from the date columns, a result of 42 days may show as 10 February 1900 instead.
  8. Copy the formula down. Drag the fill handle from C2 to the last row, or double-click it. Relative references adjust automatically, so C3 will reference A3 and B3.
  9. Test one row by hand. Pick a pair of dates where you already know the answer — a birthday, a contract start and end — and confirm the formula returns the number you expect. One correct manual check catches most unit and reference mistakes before they spread.
  10. Freeze the dates if the workbook is shared. If other people will edit the date columns, consider converting the range to an Excel Table (select the range, then Insert > Table). Structured references update automatically as rows are added. Labels vary slightly by version, but the Table command is on the Insert tab on both Windows and Mac.

Common Mistakes

Dates stored as text. This is the most frequent cause of failure. Copied data, exported CSVs and dates preceded by an apostrophe often stay as text. DATEDIF then returns #VALUE!. Fix it with Data > Text to Columns on the date column, choosing the date order that matches the source, or convert with =DATEVALUE(A2) in a helper column.

Reversed dates. If the start date is later than the end date, DATEDIF returns #NUM!. If you cannot control the order of the data, wrap it: =DATEDIF(MIN(A2,B2),MAX(A2,B2),"d") always gives a positive count.

Result formatted as a date. A value like 4213 is an ordinary number, but if the cell carries a date format it appears as a date in the year 1911. Change the format, do not change the formula.

Using "m" when you meant "ym". "m" counts all complete months between the dates, so two dates four years apart return roughly 48. "ym" returns only the leftover months after whole years, which is what most "X years, Y months" formulas need.

Expecting fractional years. DATEDIF always truncates. Twelve months and 364 days still gives 1 year. For decimal years use =(B2-A2)/365.25 instead.

Confusing DATEDIF with NETWORKDAYS. DATEDIF counts calendar days. If you need working days between two dates, NETWORKDAYS excludes weekends and is the correct function.

Tips

For a quick day count when you do not need months or years, you can simply subtract: =B2-A2 returns the same number as DATEDIF with "d", and it is easier to read. Use DATEDIF when you need month or year granularity.

To find a future or past date rather than a difference, use EDATE to add months or TODAY() inside DATEDIF to measure age from a fixed start date, for example =DATEDIF(A2,TODAY(),"y"). Be aware the result changes every day the file is opened.

Comment the formula in a nearby cell, or add a note on the header, explaining which unit code you used. DATEDIF's codes are cryptic, and a workbook that others open six months later benefits from a one-line explanation.

If you want years and months rounded to the nearest month rather than truncated, decide the rounding rule first and write it down. DATEDIF has no rounding option, so you would need to adjust the end date before calculating.

Troubleshooting

#VALUE! error. At least one argument is text, empty, or not a valid date. Check with =ISNUMBER(A2); it should return TRUE. FALSE means the cell holds text and needs converting.

#NUM! error. The start date is later than the end date. Swap the arguments or use the MIN and MAX wrapper shown above. A blank start date can also trigger it.

#NAME? error. The function name was mistyped, usually as DATEDIFF with two Fs, or a stray space crept in. Delete the formula and retype it carefully. If autocomplete keeps trying to change it, ignore the suggestion and finish typing the closing parenthesis yourself.

The formula shows as text. If the cell displays the formula rather than the result, the cell format is Text. Change it to General, then re-enter the formula by pressing F2 and Enter.

The answer is off by one day or one month. Confirm time values are not hiding in your dates. A cell containing 15/03/2024 14:00 is a different serial number from 15/03/2024. Clear any time component with =INT(A2) or format the source to show dates only after removing the time.

Nothing works and the file came from another system. Check the workbook's date system setting under File > Options > Advanced, in the section for calculating this workbook. The 1900 and 1904 date systems differ, and mixing files between them shifts every date by four years and a day.

Frequently Asked Questions

Why doesn't DATEDIF appear in Excel's function list?

It is a legacy function kept for compatibility with older spreadsheet files. It works normally but is hidden from autocomplete, so you must type it in full.

What is the difference between DATEDIF "m" and "ym"?

"m" counts every complete month across the whole span. "ym" counts only the months left over after whole years, which is why it is used alongside "y" for an age format.

Can I get years as a decimal?

Not with DATEDIF. Subtract the dates and divide, for example =(B2-A2)/365.25, to get a fractional year figure.

Does DATEDIF count the end date?

It counts the difference between the two serial numbers, so the result equals the number of full days elapsed. Two consecutive dates return 1, not 2.

How do I count working days instead of calendar days?

Use NETWORKDAYS or NETWORKDAYS.INTL, which exclude weekends and any holiday dates you list. DATEDIF does not exclude anything.

Will DATEDIF work in Excel for the web and Google Sheets?

Yes, it works in both, with the same valid unit codes. Google Sheets also hides it from autocomplete, so type the name in full there as well.