WD: How to Perform Calculations with Dates and Times |
Q122842
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.
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()))
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().
"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 : kbdta wordnt kbmacroexample winword ntword macword word6 word7 word95
Issue type : kbinfo
Technology : kbHWMAC kbOSMAC kbWordSearch kbWordMacSearch kbWord700Search kbZNotKeyword2 kbZNotKeyword3 kbWord600Mac kbWord600 kbWord600a kbWord600c kbWord700 kbWord600NT kbWord700a
|
Last Reviewed: November 5, 2000 © 2001 Microsoft Corporation. All rights reserved. Terms of Use. |