office交流网--QQ交流群号

Access培训群:792054000         Excel免费交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

Access以半周岁为单位计算年龄

2018-10-16 17:29:00
tmtony8
原创
5720

一般情况下,我们习惯说年龄都要虚岁为主。不满周岁会直接算成周岁。这个计算很简单,直接用当前年份-出生年份(year(Data())-year(出生))即可。

但是小孩子变化很快,每半周岁情况就会有大不同,所以需要以半周岁为一个单位。

如出生在2008-4-6,今天是2018-10-16 ,即合计是10年6个月1天。所以已经是10.5岁了。


详细代码:

    Dim sngAge As Single
    Dim intYear As Integer
    Dim intMonth As Integer


    If Month(Me.Text1) = Month(Date) Then
        If Day(Me.Text1) <= Day(Date) Then
            
            intYear = Year(Date) - Year(Me.Text1)
            intMonth = Month(Date) - Month(Me.Text1)
        Else
            intYear = Year(Date) - Year(Me.Text1) - 1
            intMonth = Month(Date) + 12 - Month(Me.Text1)
            
        End If
    ElseIf Month(Me.Text1) > Month(Date) Then
        intYear = Year(Date) - Year(Me.Text1) - 1
        intMonth = Month(Date) + 12 - Month(Me.Text1)
        
    Else
        intYear = Year(Date) - Year(Me.Text1)
        If Day(Me.Text1) <= Day(Date) Then
            intMonth = Month(Date) - Month(Me.Text1)
        Else
            intMonth = Month(Date) - Month(Me.Text1) - 1
            
        End If
    End If
    
    If intMonth >= 6 Then
        
        
        sngAge = intYear + 0.5
    Else
        sngAge = intYear
    End If
    
    
    Me.Text10 = "我年龄是 " & sngAge & "岁"


同理出生在2009-10-15,今天是2018-10-16 ,9年多一天,所以已经是9岁了。

    分享