Get days, months, and years between dates (2024)

In this example, the goal is to output the time between a start date and an end date as a text string that lists years, months, and days separately. For example, given a start date of 1-Jan-2018and end date of 1-Jul-2018, the result should be a string like this:

"1 years, 6 months, 0 days"

DATEDIF solution

The DATEDIF function is designed to calculate the difference between dates in years, months, and days. There are several variations available (e.g. time in months, time in months ignoring days and years, etc.) and these are set by the "unit" argument in the function. See this page on the DATEDIF function for a full list of available units.

In the example shown, we calculate years, months, and days separately, then "glue" the results together with concatenation. To get whole years, whole months, and days between the dates, we use DATEDIF like this, altering only the unit argument.

DATEDIF(B5,C5,"y") // yearsDATEDIF(B5,C5,"ym") // monthsDATEDIF(B5,C5,"md") // days

Because we want to create a string that appends the units to each number, weconcatenatethe number returned by DATEDIF to the unit name with the ampersand (&)operator like this:

DATEDIF(B5,C5,"y")&" years" // years stringDATEDIF(B5,C5,"ym")&" months" // months stringDATEDIF(B5,C5,"md")&" days" // days string

Finally, we need to extend the idea above to include spaces and commas and join everything together in one string:

=DATEDIF(B5,C5,"y")&" years, "&DATEDIF(B5,C5,"ym")&" months, " &DATEDIF(B5,C5,"md")&" days"

Implementing the LET function

The LET function (new in Excel 365) can simplify some formulas by making it possible to define and reuse variables.In order to use the LET function on this formula, we need to think about variables. The main purpose of variables is to define a useful name that can be reused elsewhere in the formula code. Looking at the formula, there are at least five opportunities to declare variables. The first two are the start date and the end date, which are reused throughout the formula. Once we assign values to start and end, we only need to refer to cell C5 and D5 one time.As a first step, we can add line breaks and then define variables for startandendlike this:

=LET( start,B5, end,C5, DATEDIF(start,end,"y")&" years, "& DATEDIF(start,end,"ym")&" months, "& DATEDIF(start,end,"md")&" days")

Notice all instances of B5 and C5 in DATEDIF have been replaced by start and end. The result from this formula is the same as the original formula above, butthe reference to B5 and C5 occurs only once. The start and end dates arethen reused throughout the formula. This makes it easier to read the formula and helps reduce errors.

The next three opportunities for variables are the results from DATEDIF for years, months, and days. If we assign these values to named variables, we can more easily combine them later in different ways, which becomes more useful in the extended version of the formula explained below. Here is the formula updated to include new variables foryears, months, and days:

=LET( start,B5, end,C5, years,DATEDIF(start,end,"y"), months,DATEDIF(start,end,"ym"), days,DATEDIF(start,end,"md"), years&" years,"&months &" months," &days &" days")

Notice we have assigned results from DATEDIF to the variables years, months, and days, and these values are concatenated together in the last line insidethe LET function. With the LET function, the last argument is the calculation or value that returns a final result.

Getting fancy with LET

Once we have the basic LET version working, we can easily extend the formula to do more complex processing, without redundantly running the same calculations over again. For example, one thing we might want to do is make the units plural or singular depending on the actual unit number. The formula below adds three new variables:ys, ms, and ds to hold the strings associated with each unit:

=LET( start,B5, end,C5, years,DATEDIF(start,end,"y"), months,DATEDIF(start,end,"ym"), days,DATEDIF(start,end,"md"), ys,years&" year"&IF(years<>1,"s",""), ms,months&" month"&IF(months<>1,"s",""), ds,days&" day"&IF(days<>1,"s",""), ys&", "&ms&", "&ds)

The strings start out singular (i.e. " year" not "years"). Then weuse the IF function to conditionally tack on an "s" only when the number is not 1. If thenumber is 1, IF returns an empty string ("") and the unit name remains singular. Because we addthe unit name to the unit number when we define ys, ms, and ds, the last step inside LET is simpler; we only need to concatenate the units with a comma and space.

See more details about the LET function here. Also, see theDetailed LET functionexamplefor another explanation of converting an existing formula to use LET.

Get days, months, and years between dates (2024)

FAQs

How to calculate days, months, and years between two dates? ›

For instance, if you subtract the hire date from today, you get the number of days as a result. If you divide the result by 365, you get the number of whole years and a decimal. So today(2/15/23) minus 8/12/14 = 3109. Then, 3109 / 365 = 8.51781.

How do I calculate days and months between dates in Excel? ›

How to calculate time between two dates in Years, Months & Days [Excel Formula]
  1. =LAMBDA(start,end, LET(y, DATEDIF(start,end,"y"), m, DATEDIF(start,end,"ym"), d, DATEDIF(start,end,"md"), y &" years "&m&" months "&d&" days")) ...
  2. =D5-D4. ...
  3. =NETWORKDAYS(D4,D5) ...
  4. =NETWORKDAYS. ...
  5. =DATEDIF(D4, D5, "m")
Jan 29, 2024

How do I calculate years and months between two dates in Excel? ›

Calculate age in accumulated years, months, and days
  1. Use DATEDIF to find the total years. In this example, the start date is in cell D17, and the end date is in E17. ...
  2. Use DATEDIF again with “ym” to find months. ...
  3. Use a different formula to find days. ...
  4. Optional: Combine three formulas in one.

What is the formula for year month and days in Excel? ›

=DATE(year,month,day)

The DATE function includes the following arguments: Year – This is a required argument. The value of the year argument can include one to four digits. Excel interprets the year argument according to the date system used by the local computer.

What is the formula for Datedif? ›

=DATEDIF(start_date,end_date,unit)

The DATEDIF function includes the following arguments: Start_date – This is a required argument. As the name suggests, it is the initial date of the period.

What to use instead of datedif in Excel? ›

How to quickly Calculate Age in Excel Without Using the Datedif Function? We can calculate age in Excel without using Datedif by using Yearfrac function.

How to use datedif in Excel? ›

DATEDIF function calculates the difference between two dates in years, months, or days. Use =DATEDIF(start_date, end_date, unit) and specify the unit of difference using a keyword. Output is an integer.

References

Top Articles
Latest Posts
Article information

Author: Carlyn Walter

Last Updated:

Views: 6410

Rating: 5 / 5 (70 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Carlyn Walter

Birthday: 1996-01-03

Address: Suite 452 40815 Denyse Extensions, Sengermouth, OR 42374

Phone: +8501809515404

Job: Manufacturing Technician

Hobby: Table tennis, Archery, Vacation, Metal detecting, Yo-yoing, Crocheting, Creative writing

Introduction: My name is Carlyn Walter, I am a lively, glamorous, healthy, clean, powerful, calm, combative person who loves writing and wants to share my knowledge and understanding with you.