How to Find Num of Days Between Dates Outside of Normal Range

Last reviewed: March 20, 1998
Article ID: Q109451
3.00 WINDOWS kbprg kbcode

The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, version 3.0

SUMMARY

To find the number of days between any two dates, you can take the difference between the values returned by the DateSerial function for two dates. However, the DateSerial function only supports dates from January 1, 100 through December 31, 9999.

To support a much wider range of dates, use the AstroDay function as in this example:

   Function AstroDay(inyear, inmonth, inday)
   ' The AstroDay function returns the Astronomical Day for any given date.
   y = inyear + (inmonth - 2.85) / 12
   AstroDay=Int(Int(Int(367*y)-1.75*Int(y)+inday)-.75*Int(.01*y))+1721119
   ' NOTE: Basic's Int function returns the integer part of a number.
   End Function

For example, the number of days between February 28, 12000 and March 1, 12000 is 2 because the year 12000 is a leap year:

   Print AstroDay(12000, 3, 1) - AstroDay(12000, 2, 28)   'Prints 2

In addition, the AstroWeekDay function defined farther below returns the day of the week, Sunday through Monday, for any given AstroDay. AstroWeekDay supports dates outside the range (January 1, 100 through December 31, 9999) of Visual Basic's WeekDay function.

MORE INFORMATION

The AstroDay function defined in this article is a modified version of the Julian date formula used by astronomers.

Visual Basic's DateSerial function returns a Variant of VarType 7 (Date) containing a date that is stored internally as a double-precision number. This number represents a date from January 1, 100 through December 31, 9999, where January 1, 1900 is 2. Negative numbers represent dates prior to December 30, 1899.

Leap years are accurately handled by both Visual Basic's DateSerial function and the Astronomical Day function (AstroDay) defined in this article.

A leap year is defined as all years divisible by 4, except for years divisible by 100 that are not also divisible by 400. Years divisible by 400 are leap years. 2000 is a leap year. 1900 is not a leap year.

Step-by-Step Example

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Double-click the form to open the code window. Add the following code to the Form Load event:

       Sub Form_Load ()
    
          form1.Show  ' Must first Show form in Load event for Print to work.
          Print AstroDay(12000, 3, 1) - AstroDay(12000, 2, 28)  'Prints 2
          Print AstroDay(-12400, 3, 1) - AstroDay(-12400, 2, 28)  'Prints 2
          Print AstroDay(12000, 3, 1) - AstroDay(-12000, 2, 28) 'Prints 8765822
          Print AstroDay(1902, 2, 28) - AstroDay(1898, 3, 1)  'Prints 1459 days
          Print AstroWeekDay(AstroDay(1993, 12, 1))  ' Prints Wednesday
          Print AstroWeekDay(AstroDay(12000, 3, 2))  ' Prints Thursday
    
          ' You can also use Visual Basic's DateSerial function as follows to
          ' find the number of days between two dates:
          Print DateSerial(1902, 2, 28) - DateSerial(1898, 3, 1)
    
          ' Visual Basic's WeekDay function returns an integer betw 1 (Sunday)
          ' and 7 (Saturday), which represents the day of the week for a date
          ' argument:
          Print "Day of week = " & Weekday(DateSerial(1898, 3, 1))
    
       End Sub
    
    

  3. In the Object box on the Form1.Frm code window, select (general). Add the following functions:

    Function AstroDay(inyear, inmonth, inday)

       ' The AstroDay function returns the Astronomical Day for any given date.
       y = inyear + (inmonth - 2.85) / 12
       AstroDay=Int(Int(Int(367*y)-1.75*Int(y)+inday)-.75*Int(.01*y))+1721119
       ' NOTE: Basic's Int function returns the integer part of an number.
       End Function
    
       Function AstroWeekDay (aday)
       ' The AstroWeekDay function returns the day of the week, Sunday through
       ' Monday, for any given AstroDay. The aday parameter must be a day
       ' number returned by the AstroDay function.
       weekdayx = (aday - 3) Mod 7
       Select Case weekdayx
       Case 0
          AstroWeekDay = "Sunday"
       Case 1
          AstroWeekDay = "Monday"
       Case 2
          AstroWeekDay = "Tuesday"
       Case 3
          AstroWeekDay = "Wednesday"
       Case 4
          AstroWeekDay = "Thursday"
       Case 5
          AstroWeekDay = "Friday"
       Case 6
          AstroWeekDay = "Saturday"
       End Select
       End Function
    
    

  4. Start the program (or press the F5 key). Close the form to end the program.


Additional reference words: 3.00
KBCategory: kbprg kbcode
KBSubcategory: PrgOther


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: March 20, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.