Results 1 to 3 of 3

Thread: Date Calculations

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Location
    Canterbury, UK
    Posts
    5

    Question Date Calculations

    Hi,

    I'm creating a program used to calculate teacher’s pensions for when they retire. Part of this program requires the reckonable service to be calculated to whole days. The brief has specified that a year is 365 days and that leap-years are to be ignored, therefore the DateDiff function is out of the question.

    I need to work out the number of years and days between two dates, and I can’t for the life of me think how to do this.

    The test criterion is that 32 years 130 days = 32.356 years.

    Can anyone help?

    Cheers,
    Adzi

  2. #2
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Date Calculations

    Internally a Date type variable is just a Double where the integer portion represents the number of days since 31st December 1899 (or a similar date).

    This means you can use simple math to do what you want...
    Code:
    Dim StartDate as Date, EndDate as date
    StartDate = "1/4/1985"
    EndDate = "30/5/2000"
    Debug.Print (Int(cDbl(EndDate)) - Int(cDbl(StartDate)) + 1) / 365
    Edit: tweaked to include the last day (+1)
    Last edited by Milk; Mar 16th, 2009 at 09:46 AM.

  3. #3
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    Re: Date Calculations

    http://support.microsoft.com/kb/211445

    Code:
    Sub DayDiff()
       Dim sDate As String
       Dim intNumDays As Integer
       On Error GoTo errhandler
       ' Prompt for a date.
       sDate = InputBox$("Enter a date in the mm/dd/yy format")
       ' Get difference between dates.
       intNumDays = CInt(DateValue(sDate) - Now())
       ' Determine if difference is past or future.
       If Sgn(intNumDays) = 1 Then
          MsgBox "Days between " + sDate + " and today :" + CStr(intNumDays)
       Else
          MsgBox "The date " + sDate + " was" + CStr(Abs(intNumDays)) _
          + " day(s) ago."
       End If
    errhandler:
       If Err > 0 Then MsgBox "Please enter a valid date."
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width