Microsoft KB Archive/122842

{|
 * width="100%"|

WD: How to Perform Calculations with Dates and Times

 * }

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, versions 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 between December 30, 1899, and the specified date, up to December 31, 4095. 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". Choose the "Statements and Functions Index" topic and select the desired command.