以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  计算年龄表达式  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=96704)

--  作者:guimingze
--  发布时间:2017/2/26 21:47:00
--  计算年龄表达式

e.DataRow("年龄") = Date.Today.Year - e.DataRow("出生年月").Year

老师其实我出生年月一栏肯定有数的  我就想直接用后面之一段公式代码  可以直接用么?


如 出生年月为1987.09  现在是2017.02   2017.02-1987.09     计算周岁

--  作者:有点色
--  发布时间:2017/2/26 22:57:00
--  

If e.DataCol.name = "出生年月" Then
    If e.newvalue <> Nothing Then

        Dim d As Date = New Date(e.newvalue.substring(0,4), e.newvalue.substring(5,2), 1)
        Dim y As Integer = d.year
        If format(d, "MMdd") <= Format(Date.Today, "MMdd") Then
            e.DataRow("年龄") = Date.Today.Year - y
        Else
            e.DataRow("年龄") = Date.Today.Year - y -1
        End If
    Else
        e.DataRow("年龄") = Nothing
    End If
End If