如何用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

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

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

Private Sub Text1_Change()

a = Format(Date, "yyyy")

Text2 = Val(a) - Val(Text1) + 1

End Sub

你自己改良下.


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

原文地址: https://outofmemory.cn/sjk/9908830.html

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

发表评论

登录后才能评论

评论列表(0条)

保存