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
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