Fungsi Hitung Umur
Fungsi ini akan menghitung umur dengan menghitung jarak antara selisih tanggal lahir dengan tanggal sekarang.
[ VB 6.0 ] & [ VBA Office ]
Public Function HitungUmur(ByVal TglLahir As Date) As String
Dim d As Integer, m As Integer, y As Integer
If TglLahir >= Now Then
HitungUmur = ""
Exit Function
End If
d = Day(Now) - Day(TglLahir)
m = Month(Now) - Month(TglLahir)
y = Year(Now) - Year(TglLahir)
If Sgn(d) = -1 Then
d = 30 - Abs(d)
m = m - 1
End If
If Sgn(m) = -1 Then
m = 12 - Abs(m)
y = y - 1
End If
HitungUmur = y & " Tahun, " & m & " Bulan, " & d & " Hari"
End Function
Dim d As Integer, m As Integer, y As Integer
If TglLahir >= Now Then
HitungUmur = ""
Exit Function
End If
d = Day(Now) - Day(TglLahir)
m = Month(Now) - Month(TglLahir)
y = Year(Now) - Year(TglLahir)
If Sgn(d) = -1 Then
d = 30 - Abs(d)
m = m - 1
End If
If Sgn(m) = -1 Then
m = 12 - Abs(m)
y = y - 1
End If
HitungUmur = y & " Tahun, " & m & " Bulan, " & d & " Hari"
End Function
[ VB .NET ]
Public Function HitungUmur(ByVal TglLahir As Date) As String
Dim d, m, y As Integer
If TglLahir >= Now Then
HitungUmur = ""
Exit Function
End If
d = Now.Day - TglLahir.Day
m = Now.Month - TglLahir.Month
y = Now.Year - TglLahir.Year
If System.Math.Sign(d) = -1 Then
d = 30 - System.Math.Abs(d)
m = m - 1
End If
If System.Math.Sign(m) = -1 Then
m = 12 - System.Math.Abs(m)
y = y - 1
End If
HitungUmur = y & " Tahun, " & m & " Bulan, " & d & " Hari"
End Function
Dim d, m, y As Integer
If TglLahir >= Now Then
HitungUmur = ""
Exit Function
End If
d = Now.Day - TglLahir.Day
m = Now.Month - TglLahir.Month
y = Now.Year - TglLahir.Year
If System.Math.Sign(d) = -1 Then
d = 30 - System.Math.Abs(d)
m = m - 1
End If
If System.Math.Sign(m) = -1 Then
m = 12 - System.Math.Abs(m)
y = y - 1
End If
HitungUmur = y & " Tahun, " & m & " Bulan, " & d & " Hari"
End Function
Contoh penggunaan :
s = HitungUmur(CDate("23/07/1989"))
Label: (Menengah), Fungsi dan Prosedur, VB .NET, VB 6.0, VBA Office
0 Komentar:
Posting Komentar
Pengunjung yang baik selalu meninggalkan jejak berupa komentar. :)
Berlangganan Posting Komentar [Atom]
<< Beranda