Rabu, Januari 10, 2007

Menghitung Umur Detail (365 hari)

'**************************************
' Name: A DateDiffX Function - Get Age a
' nd Date Difference Up to Exact Day
' Description:Enhancement for DateDiff F
' unction for the simple purpose of gettin
' g someones age or may exactly say how ol
' d is that guy in terms of Years, Month a
' nd Days he lived. Or just to check the d
' ifference between two dates.
' By: Jovy C. Lazan
'
' Inputs:This Function needs 3 functions
' (2 required and 1 optional)
> dStart As Date
> dEnd As Date
> Optional bDetailed As Boolean
'
' Returns:I just use a variant return va
' lue to return the age or the years, mont
' hs and days.
'
' Assumes:Basics of creating and using f
' unctions.
'
'This code is copyrighted and has' limited warranties.Please see http://w
' ww.Planet-Source-Code.com/vb/scripts/Sho
' wCode.asp?txtCodeId=42441&lngWId=1'for details.'**************************************


Function DateDiffX(dStart As Date, dEnd As Date, Optional bDetailed As Boolean) As Variant

Dim TotalDays
Dim Years, Months, Days


If bDetailed Then
TotalDays = dEnd - dStart
Years = Int(TotalDays / 365)
Months = Int((TotalDays - (365 * Years)) / 30)
Days = Int((TotalDays - ((365 * Years) + (30 * Months))))
DateDiffX = Years & " years, " & Months & " months, " & Days & " days"
Exit Function
End If


If Month(dEnd) > Month(dStart) Then
DateDiffX = Year(dEnd) - Year(dStart)
ElseIf Month(dEnd) < Month(dStart) Then
DateDiffX = (Year(dEnd) - Year(dStart)) - 1
ElseIf Month(dEnd) = Month(dStart) And Day(dEnd) < Day(dStart) Then
DateDiffX = (Year(dEnd) - Year(dStart)) - 1
Else
DateDiffX = Year(dEnd) - Year(dStart)
End If

End Function


' How to Use
'Private Sub Command1_Click()
'Dim output
'output = DateDiffX(Text1, Now(), True)
'MsgBox output
'End Sub

Tidak ada komentar: