## Summary

To calculate the number of months between two dates as a whole number, you can use the DATEDIF function. In the example shown, the formula in D5, copied down, is:

``````=DATEDIF(B5,C5,"m")
``````

As the formula is copied down, it returns the count of months between the start date in column B and the end date in column C.

Note: DATEDIF automatically rounds down to the nearest whole month. To round up to the nearest whole month, see the alternatives below.

## Generic formula

``=DATEDIF(start,end,"m")``

## Explanation

In this example, the goal is to calculate the number of months between two valid Excel dates. This is a curiously tricky problem in Excel because the number of days in a month varies, and the rules about how a whole month might be calculated are not obvious. In addition, there is not a modern Excel function dedicated to the task of calculating months between dates. I have no idea why. The best tool for the job is the mysterious DATEDIF function, which is kind of a black sheep in the Excel family.

### The DATEDIF function

The solutions described below are based primarily on the DATEDIF function. DATEDIF (date + dif) is designed to calculate the difference between a start date and an end date in years, months, or days. DATEDIF takes 3 arguments: start_date, end_date, and unit and the generic syntax looks like this:

``````=DATEDIF(start_date,end_date,unit)
``````

For this problem, the start dates come from column B and the end dates come from column C. For unit, we need to supply "m", because we want to calculate months between dates. See this page on the DATEDIF function for more information about the options available.

DATEDIF is a "compatibility" function that comes originally from Lotus 1-2-3. You can use DATEDIF in all current Excel versions, but you must enter the function manually — unlike other functions, DATEDIF will not appear as a suggested function in the formula bar and Excel will not help you with function arguments.

### Calculate complete whole months

In the worksheet shown below, we are using the DATEDIF function to calculate complete whole months between a start date and an end date. The start dates come from column B and the end dates come from column C. The formula in D5, copied down, is:

``````=DATEDIF(B5,C5,"m")
``````

As the formula is copied down, DATEDIF returns whole months between the start date in column B and the end date in column C. Notice that the first four results in D5:D8 are exact multiples of months, but the result in D9 has been rounded down to 5 because we are 1 day short of 6 months. Also notice that results in D13:D16 are a bit quirky, due to how DATEDIF handles end-of-month dates. For example, when the days match, the result is an exact multiple of months, as expected. However, when the days don't match, the results can be unexpected. For example, with a start date of July 31 and an end date of September 30, the result is 1, though most people would count this as 2 months. One workaround to this problem is described below.

### Calculate nearest whole months

When calculating months between two dates, DATEDIF always rounds months down to the last complete number of months. This means DATEDIF rounds a result down even when it is very close to the next whole month. To calculate months to the nearest whole month, you can adjust the formula as shown below:

``````=DATEDIF(start_date,end_date+15,"m")
``````

In this version of the formula, we add 15 days to the end date. This ensures that end dates in the second half of the month are treated like dates in the following month, effectively rounding up the final result to the next even multiple. End dates that occur in the first half of the month also increase by 15 days, but not enough to affect the normal result from DATEDIF. The screen below shows how the original DATEDIF formula compares to the modified version:

### Calculate decimal months

As shown above, DATEDIF only calculates whole months. To calculate a decimal number that represents the number of months between two dates, you can use the YEARFRAC function like this:

``````=YEARFRAC(start,end)*12
``````

YEARFRAC returns fractional years between two dates, so the result is an approximate number of months between two dates, including fractional months returned as a decimal value. The screen below shows how the original DATEDIF formula compares to the YEARFRAC version:

Keep in mind that the behavior of this formula depends on how YEARFRAC works. See this page for more information.

### Alternative formula to calculate months touched by dates

In some cases, you may want to count how many months are "touched" by a given date range. The generic formula for this calculation looks like this:

``````=(YEAR(C5)-YEAR(B5))*12+MONTH(C5)-MONTH(B5)+1
``````

This formula uses the YEAR function and the MONTH function to work out a count in two parts. First, the months associated with a year change are calculated:

``````(YEAR(end)-YEAR(start))*12 // months due to year change
``````

This code only affects the count if the start year and end year are different. If the start year and end year are the same, the result is zero. Next, the formula subtracts the start month from the end month and adds 1 to calculate the remaining months:

``MONTH(end)-MONTH(start)+1 // remaining months``

Notice this calculation means that any month difference will result in a positive number because the day of the month is completely ignored. Finally, the two parts of the formula are added together to get a total month count. The screen below shows the formula in action, compared to the original DATEDIF formula above:

As you would expect, this formula tends to increase the month count for a date range, since even a small portion of a month will be included in the result. You can see this effect if you compare results from the original DATEDIF formula in column E to results from the alternative formula in column F.

Author

### Dave Bruns

Hi - I'm Dave Bruns, and I run Exceljet with my wife, Lisa. Our goal is to help you work faster in Excel. We create short videos, and clear examples of formulas, functions, pivot tables, conditional formatting, and charts.
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://exceljet.net/formulas/get-months-between-dates", "fetch_time": 1726080249000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2024-38/segments/1725700651400.96/warc/CC-MAIN-20240911183926-20240911213926-00113.warc.gz", "warc_record_offset": 219438832, "warc_record_length": 14362, "token_count": 1415, "char_count": 6219, "score": 3.5, "int_score": 4, "crawl": "CC-MAIN-2024-38", "snapshot_type": "latest", "language": "en", "language_score": 0.8917717337608337, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00002-of-00064.parquet"}