Microsoft KB Archive/105537: Difference between revisions

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - ">" to ">")
 
(2 intermediate revisions by the same user not shown)
Line 30: Line 30:
== MORE INFORMATION ==
== MORE INFORMATION ==


WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code &quot;as is&quot; without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.<br />
WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.<br />
<br />
<br />
The following WordBasic sample macro prompts for a date in the mm/dd/yy format, and returns the number of days between the current date and the specified date.<br />
The following WordBasic sample macro prompts for a date in the mm/dd/yy format, and returns the number of days between the current date and the specified date.<br />
Line 40: Line 40:
=== Examples ===
=== Examples ===


<pre class="FIXEDTEXT">  x = DateValue(&quot;12/30/1899&quot;)  ' returns 0 or zero
<pre class="FIXEDTEXT">  x = DateValue("12/30/1899")  ' returns 0 or zero
   x = DateValue(&quot;12/31/1899&quot;)  ' returns 1
   x = DateValue("12/31/1899")  ' returns 1
   x = DateValue(&quot;12/5/1992&quot;)  ' returns 33943 </pre>
   x = DateValue("12/5/1992")  ' returns 33943 </pre>
The Sgn() function is used to determine if the difference between two dates is a negative(date in the past) or positive(date in the future) number.
The Sgn() function is used to determine if the difference between two dates is a negative(date in the past) or positive(date in the future) number.


Line 48: Line 48:
       On Error GoTo ErrHandler
       On Error GoTo ErrHandler
       ' Prompt for a date.
       ' Prompt for a date.
       otherdate$ = InputBox$(&quot;Enter a date in the mm/dd/yy format&quot;)
       otherdate$ = InputBox$("Enter a date in the mm/dd/yy format")
       ' Convert date to serial format.
       ' Convert date to serial format.
       serialdate = DateValue(otherdate$)
       serialdate = DateValue(otherdate$)
Line 55: Line 55:
       ' Determine if difference is past or future.
       ' Determine if difference is past or future.
       If Sgn(numdays) = 1 Then
       If Sgn(numdays) = 1 Then
         MsgBox &quot;The number of days between today and &quot; + otherdate$ + \  
         MsgBox "The number of days between today and " + otherdate$ + \  
         &quot; is&quot; + Str$(numdays) + &quot;.&quot;
         " is" + Str$(numdays) + "."
       Else
       Else
         MsgBox &quot;The date &quot; + enddate$ + &quot; was&quot; + Str$(Abs(numdays)) \  
         MsgBox "The date " + enddate$ + " was" + Str$(Abs(numdays)) \  
         + &quot; days ago.&quot;
         + " days ago."
       End If
       End If
       If Err &lt;&gt; 0 Then MsgBox &quot;Please enter a valid date.&quot;
       If Err <> 0 Then MsgBox "Please enter a valid date."
   End Sub </pre>
   End Sub </pre>
Additional query words: w6macroexample calculate difference
Additional query words: w6macroexample calculate difference

Latest revision as of 17:44, 20 July 2020

WD: Macro to Determine the Number of Days Between Two Dates

Q105537



The information in this article applies to:


  • Microsoft Word for Windows, versions 6.0, 6.0a, 6.0c
  • Microsoft Word for Windows, versions 7.0, 7.0a





SUMMARY

To determine the number of days between two dates, you can convert dates to serial numbers and subtract one number from the other. The DateValue() function converts a specified date, beginning with December 30, 1899, to a serial number.



MORE INFORMATION

WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

The following WordBasic sample macro prompts for a date in the mm/dd/yy format, and returns the number of days between the current date and the specified date.

The DateValue() function converts a specified date, beginning with December 30, 1899, to a serial number.

Syntax: DateValue(DateText$)

Examples

   x = DateValue("12/30/1899")  ' returns 0 or zero
   x = DateValue("12/31/1899")  ' returns 1
   x = DateValue("12/5/1992")   ' returns 33943 

The Sgn() function is used to determine if the difference between two dates is a negative(date in the past) or positive(date in the future) number.

   Sub MAIN
      On Error GoTo ErrHandler
      ' Prompt for a date.
      otherdate$ = InputBox$("Enter a date in the mm/dd/yy format")
      ' Convert date to serial format.
      serialdate = DateValue(otherdate$)
      ' Get difference between dates.
      numdays = serialdate - Today()
      ' Determine if difference is past or future.
      If Sgn(numdays) = 1 Then
         MsgBox "The number of days between today and " + otherdate$ + \ 
         " is" + Str$(numdays) + "."
      Else
         MsgBox "The date " + enddate$ + " was" + Str$(Abs(numdays)) \ 
         + " days ago."
      End If
      If Err <> 0 Then MsgBox "Please enter a valid date."
   End Sub 

Additional query words: w6macroexample calculate difference

Keywords : kbmacroexample winword word6 word7 word95
Issue type : kbhowto
Technology :


Last Reviewed: November 4, 2000
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.