Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Determining Differences Between Dates.

Determining Differences Between Dates

Written by Allen Wyatt (last updated May 4, 2024)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021


2

When you are programming 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. (Yes, that's the year 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. Note that the only difference between these two examples is the first parameter, either "ww" for weeks or "m" for months. Those aren't the only interval types you can specify within DateDiff. You can use any of these:

Interval Meaning
s seconds
n minutes
h hours
d days
y day of year (same as days)
w weeks
ww calendar weeks
m months
q quarters
yyyy years

If you want additional information about DateDiff, you can find it at this Microsoft site:

https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/datediff-function

Note:

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

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (12380) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Word 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

Endnotes by Chapter

Word allows you to easily add endnotes to your document. It even allows you to specify where those endnotes should appear ...

Discover More

Importing Based on a Partial File Name

A common task for macros is to open and process a file you want imported into your workbook. If you need to identify the ...

Discover More

Keeping a Max Value in a Cell

Sometimes it is helpful to have a cell contain the maximum value that has ever occurred within changing data. This tip ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (ribbon)

Resetting Character Formatting in a Macro

Shortcut keys are a great way to apply styles to text in a document. You can easily create a shortcut key assignment for ...

Discover More

Copying a Range of Pages in a Macro

Do you need to copy, within a macro, a range of pages? Because pages can be so fluid in Word, this can be a bit tricky. ...

Discover More

Determining If a Number is Odd or Even

Need to know if a number in a macro is odd or even? It's easy to figure out with the simple calculation shown in this tip.

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, 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}] (all 7 characters, in the sequence shown) 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 six more than 5?

2024-05-08 23:46:18

tomek

Another small difference between VBA and Worksheet function:
VBA:
=DateSerial(200, 2, 28) will evaluate to a serial number for Feb. 28, 0200

Worksheet function:
=Date(200,2,28) will evaluate to Feb. 28, 2100. Weird!

ON that weird note, both functions accept parameters that are out of range like 35 or 61 for day, and 13 or 24 for month. For day and month the negative numbers are accepted too!
e.g.,
=Date(2024,-6,1) --> June 1, 2023
=Date(2024,16,-15) --> May 16, 2025
This can be used creatively, for instance to get the date that is 44 days from the Mar. 1, 2024 =DATE(2024,3,45).


2024-05-08 23:14:05

tomek

When using date calculations in Excel you have to be aware of some idiosyncrasies, and differences between dates in the worksheet and in VBA.
Let’s first clarify the statement from the tip: “The serial number represents the number of days elapsed since a starting "base date," specifically since 1 January 100. (Yes, that's the year 100.)”

While it is true that the earliest VBA date is January 1, 0100, the serial number assigned to this date is not zero nor one but -657434. The day assigned the serial number 1 in VBA is actually Dec. 31, 1899. Hence, Jan. 1, 1900 is assigned serial number 2. This is significant, because that date (Jan. 1, 1900 ) in worksheet is assigned serial number 1. Why this discrepancy? Because in the worksheet, the year 1900 is treated as a leap year, **which it is not**.

This one-day discrepancy carries through from Jan. 1 to Feb 28, 1900. The worksheet will even accept 1900-02-29 as valid date, but there was not such a day in Gregorian calendar. Starting with March 1, 1900 both worksheet and VBA match the date serial numbers, so for most common uses this is not an issue. The conclusion of this is that you need to keep it in mind, if any of the dates you use in your calculations are in this two-month period.

VBA handles the leap-year calculation correctly, assigning negative numbers to pre-1900 dates. However, there is no **simple way**, i.e., no format, that would display a pre 1900 date in a worksheet. Negative serial numbers formatted as date show as string of “#” filling the cell. On the other hand, you can use various tricks to handle those dates, some of them described in another tip:
https://excelribbon.tips.net/T011082_Using_Early_Dates
There is a link there, that is now outdated, to another web resource; use the following instead:
https://exceluser.com/how-to-work-with-dates-before-1900-in-excel/

On top of that, there is an issue of dates in Gregorian vs. Julian calendars. The switch was first implemented in four countries and the first date in this Gregorian calendar was Oct. 15, 1582. France was next on Dec, 20,1582. Other countries followed at different times until 1927! UK, USA and Canada started on Sep. 14, 1752. In all countries that switched from Julian to Gregorian calendar there was a missing period of 10 (Italy) to 13 days (Turkey) that were removed from the calendar. Excel VBA does not account for that (it really cannot, as it is different depending on the country). It just applies Gregorian dates backwards all the way to January 1, 100. The short of it is that if you want to count number of days between two dates you have to manually account for this discrepancy. How to do this is a whole different story. Probably the best approach would be to first convert any Julian date to equivalent Gregorian one, and only then do the math.


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, 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.