Module Module1
Public Sub main()
Dim day1 As Date
Dim day2 As Date
day1 = DateTime.Parse("2000/05/05")
day2 = Now
Console.WriteLine(age(day1, day2).ToString)
Console.Read()
End Sub
'年齢を計算する
Private Function age(ByVal start As Date, ByVal last As Date) As Long
If ((start.Month * 100 + start.Day) <= (last.Month * 100 + last.Day)) Then
Return DateDiff(DateInterval.Year, start, last)
Else
Return DateDiff(DateInterval.Year, start, last) - 1
End If
End Function
End Module
▼VBA
Private Function age(ByVal start As Date, ByVal last As Date) As Long
Dim start_ar() As String
Dim last_ar() As String
start_ar = Split(start, "/")
last_ar = Split(last, "/")
If ((start_ar(1) * 100 + start_ar(2)) <= (last_ar(1) * 100 + last_ar(2))) Then
age = CInt(DateDiff("yyyy", start, last))
Else
age = CInt(DateDiff("yyyy", start, last)) - 1
End If
End Function