|
-
Mar 16th, 2009, 09:10 AM
#1
Thread Starter
New Member
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
-
Mar 16th, 2009, 09:27 AM
#2
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.
-
Mar 16th, 2009, 09:27 AM
#3
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|