Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. 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: Determining Differences Between Dates.

Determining Differences Between Dates

by Allen Wyatt
(last updated August 10, 2019)

3

When you are programming Excel macros, you should know that dates are stored internally, within variables, as serial numbers. The serial number represents the number of days elapsed since a starting "base date," specifically since 1 January 100. This means that you can perform math with the serial numbers, if desired. You can, for instance, find the number of days between two dates by simply subtracting the dates from each other.

If you want to get fancier in your date calculations, you can use the DateDiff function. This function allows you, for instance, to determine the number of weeks or months between two dates. In order to use the function to find this type of information, you would do as follows:

iNumWeeks = DateDiff("ww", dFirstDate, dSecondDate)
iNumMonths = DateDiff("m", dFirstDate, dSecondDate)

The first line determines the number of weeks between the two dates, and the second determines the number of months between them.

Remember that the DateDiff function is a macro (VBA) function, not a worksheet function. Excel handles a range of dates in worksheets that begin with January 1, 1900. In VBA, however, dates can begin (as already noted) in the year 100. That means that macros can handle a much larger range of dates, including dates prior to those handled natively by Excel.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9046) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. You can find a version of this tip for the older menu interface of Excel here: Determining Differences Between Dates.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Deleting MRU Entries

By default Word dutifully keeps track of the different files you've worked on in the past. If you want to delete an entry ...

Discover More

Removing Return Addresses

Word allows you to print return addresses on your envelopes. You may run across a scenario where the return address is ...

Discover More

Searching for Comment Marks

Got a bunch of comments in your document? You can easily jump from one comment to the other by using the Object Browser, ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Reorganizing Data

If you need to consolidate a single column of data into multiple columns of data, you'll love this macro. It provides a ...

Discover More

Finding the Last-Used Cell in a Macro

Ever wonder what the macro-oriented equivalent of pressing Ctrl+End is? Here's the code and some caveats on using it.

Discover More

Relative VBA Selections

Need to select a cell using a macro? Need that selection to be relative to the cell you currently have selected? Here are ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 - 6?

2019-08-12 11:44:32

Roy

Datedif() ALSO has 2-3 failures available. 2-3 of the options give wrong answers.

This is a known defect and lack of interest by Excel in fixing it is probably why they don't document it. Likely they left it in though for any spreadsheets written using it by trusting souls. Those souls only had to fix the formulas that used the defective operations.

I don't remember the defective options off-hand but I'm sure the internet does:

https://support.office.com/en-us/article/datedif-function-25dba1a4-2812-480b-84dd-8b32a451b35c says the "MD" option may give wrong naswers. It also says it was intended for compatibility with 1-2-3.

Other sites add information and it looks like it picks the second month entered as "the" month toi base to calculation on which causes difficulties if the two months involved do not have the same number of days. For example, February and any other month will cause 2-3 days to be added to the answer expected if January is the month of the second date. And vice versa. And some palces suggest it is less straightforward than that, either way, it is not only an obvious difficulty programmers should have expected (and MAY have, but kept it so because the 1-2-3 function worked (worongly) that way - or not, maybe the 1-2-3 function worked perfectly).

In any case, it has at least the one defect and I seem to recall there was a second, but you know how recall goes as one gets old...

In any case though, most all of it works so if ya wanna use it, it's there to be used!


2019-08-12 11:33:22

Dave

Just a caution...

When working with dates, always consider the possibility that your date serial numbers may have fractional portions. That's because everything to the right of the decimal point represents time with a given integer date. It's very slick.

Some examples of date serial numbers and what they represent:
43689.000000 August 12, 2019 -- specifically at 12:00 midnight at the start of the day.
43689.500000 August 12, 2019 at noon
43689.750000 August 12, 2019 at 6:00 pm (18.00)
43689.999988 August 12, 2019 at 11:59 pm (23.59.59)

Depending on your needs and your data, it may be good to truncate date values to make them integers before subtracting one from the other.

Also, when working with dates, you need to decide whether to add one to the result or not, depending on your needs.

For example, if both of your dates are August 12, do you want your result to be zero days or one day? Merely subtracting the values will give you zero days, but that may not match your intent. In that case, modify your formula to add 1 to the result.

Similarly, if your two dates are August 12 and 14, do you want the result to be 2 days of 3?


2019-08-10 14:46:05

Dean Cardno

Datedif is also available as a worksheet function (at least up to Excel 2013). MS stopped documenting it early on, for some reason, but it has remained in the product. The syntax is different:
= Datedif(<first date>, <second date>, <format>), where 'format is "Y" for years, "M" for months, "YM" for months ignoring full years, and "MD" for days ignoring full months. A formula of:
=DATEDIF(H16, H22, "Y") & " Years, " & DATEDIF(H16, H22, "YM") & " Months, and "& DATEDIF(H16, H22, "MD") & " days"

will return a result like:
3 Years, 2 Months, and 20 days


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.