探讨,同northwolves(野性的呼唤)、楼主:
我写的函数的结果和northwolves(野性的呼唤)是一样的,我就不贴了,大同小异,我的问题是:
Private Sub Command1_Click()
MsgBox age(CDate("2002年10月31日"))
MsgBox age(CDate("2002年11月30日"))
MsgBox age(CDate("2002年12月31日"))
End Sub
Private Function Sjcz(id As Date, dqsj As Date, rq As String)
If Day(dqsj) - Day(id) >= 0 Then
ts = Day(dqsj) - Day(id)
If Month(dqsj) - Month(id) > 0 Then
ns = Year(dqsj) - Year(id)
ys = Month(dqsj) - Month(id)
Else
ys = (12 - Month(dqsj)) + Month(id)
ns = Year(dqsj) - Year(id) - 1
End If
Else
If Month(dqsj) - Month(id) > 0 Then
ns = Year(dqsj) - Year(id)
Select Case Month(id)
Case 1, 3, 5, 7, 8, 10, 12
ts = 31 - Day(id) + Day(dqsj)
Case 4, 6, 9, 11
ts = 30 - Day(id) + Day(dqsj)
Case 2
nd = Year(id) Mod 4
If nd = 0 Then
ts = 29 - Day(id) + Day(dqsj)
Else
ts = 28 - Day(id) + Day(dqsj)
End If
End Select
ys = Month(dqsj) - Month(id) - 1
Else
ns = Year(dqsj) - Year(id) - 1
Select Case Month(id)
Case 1 Or 3 Or 5 Or 7 Or 8 Or 10 Or 12
ts = 31 - Day(id) + Day(dqsj)
Case 4 Or 6 Or 9 Or 11
ts = 30 - Day(id) + Day(dqsj)
Case 2
nd = Year(id) Mod 4
If nd = 0 Then
ts = 29 - Day(id) + Day(dqsj)
Else
ts = 28 - Day(id) + Day(dqsj)
End If
End Select
ys = (12 - Month(dqsj)) + Month(id) - 1
End If
End If
Private Sub Command1_Click()
MsgBox age(CDate("1902年12月1日"))
MsgBox age(CDate("2004年1月8日"))
'MsgBox age(CDate("1912年10月23日"))
End Sub
Function age(ByVal birthday As Date) As String
If birthday > Date Then MsgBox "err!you can't be born in the future!": Exit Function
Dim nday As Integer, nmonth As Integer, nyear As Integer ' today
Dim bday As Integer, bmonth As Integer, byear As Integer 'birthday
Dim theday As Integer, themonth As Integer, theyear As Integer 'datediff
nday = Day(Date)
nmonth = Month(Date)
nyear = Year(Date)
bday = Day(birthday)
bmonth = Month(birthday)
byear = Year(birthday)
If nday >= bday Then
theday = nday - bday
Else
theday = Format(DateSerial(Year(Date), Month(Date), nday - bday), "d")
nmonth = nmonth - 1
End If
If nmonth >= bmonth Then
themonth = nmonth - bmonth
Else
themonth = nmonth + 12 - bmonth
nyear = nyear - 1
End If
theyear = nyear - byear
age = theyear & " 岁 " & themonth & " 月 " & theday & " 天"
修正:
Function age(ByVal birthday As Date) As String
Dim theday As Date
theday = DateSerial(Year(Date) - Year(birthday), Month(Date) - Month(birthday), Day(Date) - Day(birthday))
age = Format(theday, "yyyy岁零m月d天")
If Left(age, 1) = "0" Then age = Right(age, Len(age) - 1)
End Function
MsgBox age(CDate("2002年12月1日"))
'MsgBox age(CDate("1994年7月12日"))
'MsgBox age(CDate("1912年10月23日"))
End Sub
Function age(ByVal birthday As Date) As String
Dim theday As Date
theday = DateSerial(Year(Date) - Year(birthday), Month(Date) - Month(birthday), Day(Date) - Day(birthday))
age = Format(theday, "yy岁零m月d天")
If Left(age, 1) = "0" Then age = Right(age, Len(age) - 1)
End Function