Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Day issue with Function 3

Status
Not open for further replies.

traycee

IS-IT--Management
Sep 6, 2001
245
US
Hi,
I'm using the following function and don't exactly understand why it isn't working. I do know that days are a pain to count. Example: 9/27/2004 to 1/21/2005 should be 3 months and 25 days but it comes up 3 months and 26 days. Yet other dates come up right...sucn as 11/4/2004 to 1/12/2005 comes out to 2 months and 8 days which is correct. I'm thinking this may be something I can't correct. Is there a better way to do this. The function I'm using follows:

Function YearMonthDate(DateAdmittedToProgram As Date, DischageDate As Date) As String
Dim Y As Integer
Dim M As Integer
Dim D As Integer
Dim Temp1 As Date
Temp1 = DateSerial(Year(DischageDate), month(DateAdmittedToProgram), Day(DateAdmittedToProgram))
Y = Year(DischageDate) - Year(DateAdmittedToProgram) + (Temp1 > DischageDate)
M = month(DischageDate) - month(DateAdmittedToProgram) - (12 * (Temp1 > DischageDate))
D = Day(DischageDate) - Day(DateAdmittedToProgram)
If D < 0 Then
M = M - 1
D = Day(DateSerial(Year(DischageDate), month(DischageDate) + 1, 0)) + D + 1
End If
YearMonthDate = Y & " years " & M & " months " & D & " days"
End Function

Thanks :)
 
Thanks...couldn't have done it without y'all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top