WD: How to Perform Calculations with Dates and TimesLast reviewed: February 5, 1998Article ID: Q122842 |
The information in this article applies to:
SUMMARYThis 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.
EXAMPLESTo 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 SubTo 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 INFORMATIONThe 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 34335The 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.69097222222222Date 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
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |