Written by Allen Wyatt (last updated August 9, 2025)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365
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.
Even so, you need to be aware that historical dates can have weird quirks associated with them. For instance, in Great Britain the calendar went from September 2, 1752, to September 14, 1752, skipping all the days between. This was done in conjunction with Great Britian adopting the Gregorian calendaring system. Quirks like this can vary by city or country, and DateDiff doesn't take them into account in its calculations.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9046) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Determining Differences Between Dates.
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!
Named ranges are a great capability provided by Excel. You can define all sorts of named ranges in a workbook, but how do ...
Discover MoreIf your macro processes information on a number of worksheets, chances are good that you need your macro to figure out ...
Discover MoreIf you have a large, complex workbook, you may want to make sure that it is always calculated manually instead of ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2025-08-09 09:37:03
Alex Blakenburg
Ouch, I am not sure that the above adequately describes how VBA DateDiff works since it behaves differently to the Excel DateDif.
eg iNumMonths = DateDiff("m", dFirstDate, dSecondDate) counts months each time the date crosses over into a new month. It does not count completed months as is the case for Excel's DateDif.
• VBA DateDiff("m", 31-Jan-2025,02-Feb-2025) would give you 1 month
• Excel DateDif( 31-Jan-2025,02-Feb-2025),"m") would give you 0 completed months.
Similarly VBA DateDiff using "yyyy" would return 1 year if the start date is Dec 2024 and the End Date is Jan 2025 because if crosses over into a new year.
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 © 2025 Sharon Parq Associates, Inc.
Comments