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.
Best-Selling VBA Tutorial for Beginners Take your Excel knowledge to the next level. With a little background in VBA programming, you can go well beyond basic spreadsheets and functions. Use macros to reduce errors, save time, and integrate with other Microsoft applications. Fully updated for the latest version of Office 365. Check out Microsoft 365 Excel VBA Programming For Dummies today!
The macro programming language used in Excel gives you a great many tools that allow you to modify the way that Excel ...
Discover MoreMacros can be used to change the formatting of your worksheet, if desired. One change you might want to make is to the ...
Discover MoreWhen you open multiple workbooks, the way in which Excel sizes them is not the best for your needs. This tip looks at a ...
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