我这里有一个获取年龄的vb.net代码,但它不打印实际日期例如:今天是2020年6月4日,我的生日是1998年6月5日,输出的年龄将是22岁,即使正确的年龄是21岁:请参阅下面的代码以供参考
Dim birthday As String = "06/05/1998"
birthday = Format(DateValue(birthday), "MM/dd/yyyy")
Response.Write(DateDiff(DateInterval.Month, DateValue(birthday), Now()) / 12)
'Output : 22发布于 2020-06-04 14:02:56
Private Function GetAge(dateOfBirth As Date) As Integer
dateOfBirth = dateOfBirth.Date
Dim age = CInt(DateDiff(DateInterval.Year, dateOfBirth, Date.Today))
dateOfBirth = dateOfBirth.AddYears(age)
If dateOfBirth > Date.Today Then
age -= 1
End If
Return age
End Function发布于 2020-06-04 13:36:38
Dim birthday As Date = CDate("06/04/1998")
Dim year As Byte = DateDiff(DateInterval.Year, birthday, Now())
If Now.Month > birthday.Month Then
year -= 1
ElseIf Now.Month = birthday.Month And Now.Date > birthday.Date Then
year -= 1
End If
Response.Write(year)https://stackoverflow.com/questions/62187300
复制相似问题