vb怎样计算年龄

vb怎样计算年龄,第1张

我写的代码,你参考一下把。

Function age(ByVal till As Date, ByVal birth As Date) As String

'计算年龄,几岁几个月几天,till:截止日期,樱歼birth生日

Dim year As Integer = 0 '年

year = till.Year - birth.Year

If year <0 Then

Return ""

End If

If year = 0 Then

''

End If

If year >0 Then

If till.Month <birth.Month Then

'还差一个脊知冲月才到一年

year = year - 1

ElseIf till.Month >birth.Month Then

'超过一个月

ElseIf till.Month = birth.Month Then

'同一个月

If till.Day <birth.Day Then

'从天数看还没到一个月,不 *** 作

year = year - 1

ElseIf till.Day >= birth.Day Then

'到了天数,或超了天猛则数

End If

End If

End If

Dim month As Integer = till.Month - birth.Month

If month <0 Then

month = 12 + month

If till.Day <birth.Day Then

'从天数看还没到一个月,不 *** 作

month = month - 1

ElseIf till.Day >= birth.Day Then

'到了天数,或超了天数,不 *** 作

End If

ElseIf month = 0 Then

''同一个月

If till.Day <birth.Day Then

'从天数看,已经超过了今天的日期,而且肯定不是同一年

month = 11

ElseIf till.Day >= birth.Day Then

'到了天数,或超了天数,同一个月,还没过生日

End If

ElseIf month >0 Then

If till.Day <birth.Day Then

'从天数看还没到一个月,不 *** 作

month = month - 1

ElseIf till.Day >= birth.Day Then

'到了天数,或超了天数,不 *** 作

End If

End If

Dim day As Integer = till.Day - birth.Day

If day >= 0 Then

'ok

ElseIf day <0 Then

day = Date.DaysInMonth(birth.Year, birth.Month) - birth.Day + till.Day

End If

Return year &"岁" &month &"月" &day &"天"

End Function

dim s as string,y as string i as long

s=xxxxxx19920315xxxx

y=mid(s,7,4)

从身份z的第七位取4个拍基蠢

i=year(now())-val(y)+1

now()函数是获得现在的电脑的时间,year是把now的年锋握份取出,val是把字符串转换成数字,因为刚生下来就是1岁所袭陪以要加1

msgbox i

我写的代码,你参考一下把。

Function age(ByVal till As Date, ByVal birth As Date) As String

'计算年龄,几岁几个月几天,till:截止日期,birth生日

Dim year As Integer = 0 '年

year = till.Year - birth.Year

If year <0 Then

Return ""

End If

If year = 0 Then

''

End If

If year >0 Then

If till.Month <birth.Month Then

'还差一个月才到一年

year = year - 1

ElseIf till.Month >birth.Month Then

'超过一个月

ElseIf till.Month = birth.Month Then

'同一个月

If till.Day <birth.Day Then

'从天数看还没到一个月,不 *** 作

year = year - 1

ElseIf till.Day >= birth.Day Then

'到了天数,或超了天数

End If

End If

End If

Dim month As Integer = till.Month - birth.Month

If month <0 Then

month = 12 + month

If till.Day <birth.Day Then

'从天数看还没到扰册一个月,不 *** 作

month = month - 1

ElseIf till.Day >= birth.Day Then

'到了天数,或超了天数,不 *** 作

End If

ElseIf month = 0 Then

'伍兄'同一个月

If till.Day <birth.Day Then

'从天数看,已经超过了今天的腔李袭日期,而且肯定不是同一年

month = 11

ElseIf till.Day >= birth.Day Then

'到了天数,或超了天数,同一个月,还没过生日

End If

ElseIf month >0 Then

If till.Day <birth.Day Then

'从天数看还没到一个月,不 *** 作

month = month - 1

ElseIf till.Day >= birth.Day Then

'到了天数,或超了天数,不 *** 作

End If

End If

Dim day As Integer = till.Day - birth.Day

If day >= 0 Then

'ok

ElseIf day <0 Then

day = Date.DaysInMonth(birth.Year, birth.Month) - birth.Day + till.Day

End If

Return year "岁" month "月" day "天"

End Function


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12424591.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存