WD: How to Perform Calculations with Dates and Times

Last reviewed: February 5, 1998
Article ID: Q122842
The information in this article applies to:
  • Microsoft Word for Windows, versions 6.0, 6.0a, 6.0c
  • Microsoft Word for Windows 95, versions 7.0, 7.0a
  • Microsoft Word for the Macintosh, version 6.0
  • Microsoft Word for Windows NT, version 6.0, 6.0.1, 6.0.1a

SUMMARY

This article provides several examples of how to use WordBasic macros to perform calculations on dates and times. Word does this by first converting a date or time into a "serial number." Word then uses the serial number in calculations and converts it back into a standard date or time format.

EXAMPLES

To insert a date seven days from the current date:

   Insert Date$(Today() + 7)

To insert "July 1, 1925" or "07/01/25" (without the quotation marks) (because this date is one-half of December 31, 1950, in number of days when December 30, 1899 is 0 [zero]):

   Insert Date$(DateSerial(50, 12, 31) / 2)

To calculate how many days old you are, where mm/dd/yy is your birthdate, and display that number in a message box:

   Sub Main
    MsgBox Str$(Today() - DateValue("mm/dd/yy"))
   End Sub

To insert 6:00 AM (because 6:00 AM is one-half of 12:00 PM):

   Insert Time$(TimeValue("12:00") / 2)

To insert the current date and time:

   Insert Date$(Now()) + ", " + Time$(Now())

To calculate the last day of the previous month:

   Insert Date$(Today() - Day(Now()))

MORE INFORMATION

The date serial number is a whole number that represents the number of days since the date December 31, 1899. Here are some examples:

   Date                    Serial Number
   ----                    -------------

   December 30, 1899                   0
   December 31, 1899                   1
   January 1, 1900                     2
   January 1, 1994                 34335

The time serial number is a decimal number between 0 and 0.99998842592593 that represents a time from 12:00:00 AM to 11:59:59 PM. For example:

   Time                    Serial Number
   ----                    -------------

   12:00 AM                0.0
   06:00 AM                0.25
   12:00 PM                0.50
   04:35 PM                0.69097222222222

Date functions include Date$(), DateSerial(), DateValue(), Day(), Days360(), Today(), Month(), Weekday(), and Year().

Time functions include Time$(), TimeSerial(), TimeValue(), Hour(), Minute(), Second(), and Now().

NOTE: The date format is determined by the "DateFormat=" line in the [Microsoft Word] section of the WINWORD6.INI file (Windows), in the Word Settings (6) file (Macintosh), or in the Registry (Windows NT). (In Windows, if there is no "DateFormat=" line, Date$() uses the "sShortDate" setting in the [intl] section of the WIN.INI file.) You can use the SetPrivateProfileString macro to change the current date format.

For more information on these functions and for examples of specific WordBasic commands, refer to the "Microsoft Word Developer's Kit," or choose Contents from the Help menu, then choose "Programming with Microsoft Word" (without the quotation marks). Choose the "Statements and Functions Index" topic and select the desired command.

REFERENCES

"Microsoft Word Developer's Kit," version 6.0, pages 319-322, 511-512, 595- 596, 610, 659, 733-735, 812 and 820


Additional query words: seriel macro basic
Keywords : kbmacroexample macword ntword winword word6 word7 word95 wordnt
Version : WINDOWS:6.0,6.0a,6.0c,7.0,7.0a;MACINTOSH:6.0,6.0.1,6.0.1a
Platform : MACINTOSH WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 5, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.