Inserting the Time Remaining Until a Target Date and Time

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


Michael can use a field to insert a date and time into a document. He would like, however, to insert the "time remaining" until some date and time in the future. For instance, if the future date and time is 6:00 am on December 1, the "time remaining" would show as "DDD:HH:MM (Dec. 1, 2022 6:00 am)," with DDD being days, HH being hours, and MM being minutes.

This cannot be done using fields, but it can be done using a macro. And, you can make it at least semi-automatic if your macro relies upon the use of document variables. Consider the following rather short macro:

Sub CalcDateInterval()
    Dim dTargetDate As Date
    Dim dFromNow As Date
    Dim sTemp As String
    Dim v As Variable

    dTargetDate = DateValue("1 December 2022") + TimeValue("6:00:00 AM")
    dFromNow = dTargetDate - Now()

    sTemp = Format(Int(dFromNow), "#,##0") _
      & Format(dFromNow, ":hh:mm") & " " _
      & Format(dTargetDate, "(mmm. d, yyyy h:mm am/pm)")

    For Each v In ActiveDocument.Variables
        If v.Name = "CountDown" Then v.Delete
    Next v
    ActiveDocument.Variables.Add Name:="CountDown", Value:=sTemp
End Sub

The macro uses the future date and time (set in the dTargetDate variable) to calculate the number of days, hours, and minutes between now and then. This information is then formatted into the desired text string and placed into a document variable named CountDown.

Since the desired information is in a document variable, you can now use a field to reference the value of that variable. Here is the format of the field you would use:

{ DOCVARIABLE "CountDown" }

Every time you want to recalculate the countdown value, just re-run CalcDateInterval and update your fields. You could, if desired, set up CalcDateInterval as part of an event handler (so it is run automatically based on whatever event is being handled), but you will still need to update the fields to see the result in your document.

Remember, as well, that the countdown value (the string put together according to your desires) will not be "live"; it won't show an actual countdown that updates every minute. The value returned by the DOCVARIABLE field will only update when you update fields, and that is predicated on the updating of the actual document variable by using the CalcDateInterval macro.

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 (12822) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021.

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

Quickly Formatting Footers in Documents with Many Sections

Need to adjust all the footers or headers in a document that uses lots of them? It's easy to do if you understand how the ...

Discover More

Getting Rid of Section Breaks, but Not Section Formatting

Word allows you to change the character of how your pages are designed by using multiple sections in a document. If you ...

Discover More

Unwanted Hyperlinks

Tired of having Excel convert what you type into active hyperlinks? Here are things you can do to undo Excel's ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (ribbon)

Trimming Spaces from Strings

When processing text with a macro, you often need to remove extraneous spaces from the text. VBA provides three handy ...

Discover More

Creating a New Document in VBA

When working with documents in a macro, it makes sense that you may need to create a document from time to time. Here's ...

Discover More

Cleaning Up Text in a Macro

Need to remove extraneous characters from a text string? VBA makes it easy through the CleanString method, described in ...

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 8 - 5?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


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.