Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Unique Date Displays.
Written by Allen Wyatt (last updated April 20, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021
Jon requested help on how to subtract two dates and display the result such that the years were on the left of the decimal and the months on the right. Thus, if you subtracted January 7, 1991 from August 12, 2019, the result would be 28.7.
The easiest way to do this is to simply do your date subtractions as regular, and then use a custom format to display the result. For instance, if the lower date were in cell A2 and the higher date in B2, you could use the following formula in C2:
=B2-A2
You would then follow these steps to format the display of the result in cell C2:
Figure 1. The Number tab of the Format Cells dialog box.
The result is that C2 shows the number of years to the left of the decimal and the number of months to the right. The problem with this is that it will always vary the number of months from 1 to 12, rather than 0 to 11, as one would expect if you were looking for elapsed months. (Hence, the result of 28.8 in C2.)
To overcome this, you could enter the following formula in cell C2:
=(YEAR(B2)-YEAR(A2))+(MONTH(B2)-MONTH(A2))/100
This formula returns the number of years on the left of the decimal and the number of months on the right. (The result is 28.07 in C2.) There are two potential problems with this approach. First, it won't give the right result if the date in A2 is after the date in B2, particularly if the dates are in different years. (You'll end up with negative years that way.) The second problem is that the months are always expressed using two decimal places, and you may want to not have any leading zeroes.
Both issues can be solved by modifying your formula to use the DATEDIF function, returning the result as text instead of as a numeric value, in this manner:
=IF(A2 < B2, DATEDIF(A2,B2,"y") & "." & DATEDIF(A2,B2,"ym"), DATEDIF(B2,A2,"y") & "." & DATEDIF(B2,A2,"ym"))
There are two variations of the DATEDIF function in this formula, wrapped in an IF function. The reason is because DATEDIF expects the first parameter to be earlier than the second parameter, and the IF function checks to make sure this is the case.
The result in C2 is now 28.7, but it is a text string, not a numeric value. It is necessary to return a text string because a difference of 1 and 10 months would both return as something like 28.1 if the result were numeric.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11057) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Unique Date Displays.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
When working with dates and the relationship between dates, Excel provides a variety of worksheet functions that may ...
Discover MoreNeed to figure out if one date is between two other dates? There are a wide variety of formulaic approaches you could use ...
Discover MoreExcel makes it easy to import information created in other programs. Converting the imported data into something you can ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2024-04-23 10:54:47
J. Woolley
Re. my earliest comment below, all 6 the formulas ending with a divisor of 10 should instead have a divisor of 100 and displayed using Number format with 2 decimal places. Obviously a month can have more than 1 digit. My bad.
Re. my most recent comment below, the first formula's result should be formatted as Number with 2 decimal places, not General. The formulas that use a custom fractional format are okay even when MONTH(B2) <= MONTH(A2), but if
B2 < A2 the result will be negative.
2024-04-22 11:31:34
J. Woolley
Re. my previous comment below, here is a simpler formula for C2 resulting in 28.07 when formatted as General:
=TEXT(B2-A2,"yy.mm")-1/100
Jon wanted the numeric value to be 28.7, but 28.07 might be a better representation of 28 years and 7 months.
Here is a another formula
=(B2-A2)/365.25
with the numeric result 28.59411362 displayed as
28 + 7/12
when custom formatted as fractional
# "+" ?/12
The following formula is more accurate (see the Tip):
=(YEAR(B2)-YEAR(A2))+(MONTH(B2)-MONTH(A2))/12
In this case the decimal result 28.58333333 will display as
28 + 7/12
using the custom fractional format.
2024-04-21 11:40:10
J. Woolley
If A2 is January 7, 1991, B2 is August 12, 2019, and C2 is =B2-C2 formatted as yy.m, then C2 is 28.8 rather than 28.7 as desired because m is 1 to 12 instead of 0 to 11. Here are two alternate formulas for C2 resulting in 28.7 when formatted as General:
=LET(x, TEXT(B2-A2,"yy.mm"), y, INT(x), z, RIGHT(x,2), y+(z-1)/10)
=LET(x, TEXT(B2-A2,"yy.mm"), INT(x)+(RIGHT(x,2)-1)/10)
The LET function requires Excel 2021+ (or 365, of course). Here is the equivalent formula for older Excel versions:
=INT(TEXT(B2-A2,"yy.mm"))+(RIGHT(TEXT(B2-A2,"yy.mm"),2)-1)/10
Notice how text is automatically converted to numeric for arithmetic.
My Excel Toolbox includes the following function:
=TimeDif(Start, Finish, [Approximate], [Conversational])
This returns the difference between two time values or two date values (with or without time-of-day) as text. If Start > Finish, they will be exchanged so their difference is a positive value.
For example, if A2 is January 7, 1991, and B2 is August 12, 2019:
=TimeDif(A2,B2) returns "28 yr 07 mth 05 day 00 hr 00 min 00 sec"
=TimeDif(A2,B2,TRUE) returns "about 28 and a half years"
=TimeDif(A2,B2,,TRUE) returns "28 years 7 months and 5 days"
The first formula's result is formatted with 2 digits for each numeric value and 2 spaces after each label, but if the year result is more than 99 its numeric digits will expand appropriately. Assuming the year result does not require more than 2 digits, the following equivalent formulas will return 28.7 when formatted as General:
=LET(x, TimeDif(A2,B2), y, MID(x,1,2), z, MID(x,8,2), y+z/10)
=LET(x, TimeDif(A2,B2), MID(x,1,2)+MID(x,8,2)/10)
=MID(TimeDif(A2,B2),1,2)+MID(TimeDif(A2,B2),8,2)/10
See https://sites.google.com/view/MyExcelToolbox/
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments