一个日期跟系统日期对比,算出人的岁数是多少岁几个月几日
例如:2002年12月1日和2004年1月6日比较,得到一岁零一个月五日,代码怎么写?

解决方案 »

  1.   

    提点意见eg:Format(DateSerial(0, 0, DateDiff("D", #12/01/2002#, #01/06/2004#)), "YY 岁 M月 D日")
      
    这个基本可行,很多日期比较都可以,但还是有误差,可以试着改改。
      

  2.   

    Private Sub Command1_Click()MsgBox age(CDate("2002年12月1日"))
    'MsgBox age(CDate("1994年7月12日"))
    'MsgBox age(CDate("1912年10月23日"))
    End Sub
    Function age(ByVal birthday As Date) As String
    Dim theday As Date
    theday = DateSerial(Year(Date) - Year(birthday), Month(Date) - Month(birthday), Day(Date) - Day(birthday))
    age = Format(theday, "yy岁零m月d天")
    If Left(age, 1) = "0" Then age = Right(age, Len(age) - 1)
    End Function
      

  3.   

    to northwolves(野性的呼唤): 试试:
      MsgBox age(CDate("1902年12月1日"))
    to  paoluo:
     试试:
    Debug.Print Format(DateSerial(0, 0, DateDiff("D", #12/1/1902#, #1/6/2004#)), "YY 岁 M月 D日")修正:
    Function age(ByVal birthday As Date) As String
        Dim theday As Date
        theday = DateSerial(Year(Date) - Year(birthday), Month(Date) - Month(birthday), Day(Date) - Day(birthday))
        age = Format(theday, "yyyy岁零m月d天")
        If Left(age, 1) = "0" Then age = Right(age, Len(age) - 1)
    End Function
      

  4.   

    修正后也不对,重写:Private Sub Command1_Click()
    MsgBox age(CDate("1902年12月1日"))
    MsgBox age(CDate("2004年1月8日"))
    'MsgBox age(CDate("1912年10月23日"))
    End Sub
    Function age(ByVal birthday As Date) As String
    If birthday > Date Then MsgBox "err!you can't be born in the future!": Exit Function
        Dim nday As Integer, nmonth As Integer, nyear As Integer ' today
            Dim bday As Integer, bmonth As Integer, byear As Integer 'birthday
                   Dim theday As Integer, themonth As Integer, theyear As Integer 'datediff
            nday = Day(Date)
            nmonth = Month(Date)
            nyear = Year(Date)
            bday = Day(birthday)
            bmonth = Month(birthday)
            byear = Year(birthday)
            If nday >= bday Then
            theday = nday - bday
            Else
            theday = Format(DateSerial(Year(Date), Month(Date), nday - bday), "d")
            nmonth = nmonth - 1
            End If
                    If nmonth >= bmonth Then
            themonth = nmonth - bmonth
            Else
            themonth = nmonth + 12 - bmonth
            nyear = nyear - 1
            End If
           theyear = nyear - byear
        age = theyear & " 岁 " & themonth & " 月 " & theday & " 天"
      
    End Function
      

  5.   

    探讨,同northwolves(野性的呼唤)、楼主:
    我写的函数的结果和northwolves(野性的呼唤)是一样的,我就不贴了,大同小异,我的问题是:
    Private Sub Command1_Click()
        MsgBox age(CDate("2002年10月31日"))
        MsgBox age(CDate("2002年11月30日"))
        MsgBox age(CDate("2002年12月31日"))
    End Sub2002年10月31日到2002年11月30日,及2002年11月30日到2002年12月31日到底算不算一个整月?现在,我是糊涂了。如果,答案符合楼主要求的话,就什么都不用说了,如果不是,这个问题就复杂了。
      

  6.   

    如何用SQL实现呢??
    不如在sql server服务端实现。
      

  7.   

    http://www.1000vb.net/html/gb/exp2/QQExp053.html#datediff
      

  8.   

    Private Function Sjcz(id As Date, dqsj As Date, rq As String)
    If Day(dqsj) - Day(id) >= 0 Then
        ts = Day(dqsj) - Day(id)
        If Month(dqsj) - Month(id) > 0 Then
            ns = Year(dqsj) - Year(id)
            ys = Month(dqsj) - Month(id)
        Else
            ys = (12 - Month(dqsj)) + Month(id)
            ns = Year(dqsj) - Year(id) - 1
        End If
        
    Else
        If Month(dqsj) - Month(id) > 0 Then
            ns = Year(dqsj) - Year(id)
            Select Case Month(id)
                Case 1, 3, 5, 7, 8, 10, 12
                    ts = 31 - Day(id) + Day(dqsj)
                Case 4, 6, 9, 11
                    ts = 30 - Day(id) + Day(dqsj)
                Case 2
                    nd = Year(id) Mod 4
                    If nd = 0 Then
                        ts = 29 - Day(id) + Day(dqsj)
                    Else
                        ts = 28 - Day(id) + Day(dqsj)
                    End If
            End Select
            ys = Month(dqsj) - Month(id) - 1
        Else
            ns = Year(dqsj) - Year(id) - 1
            Select Case Month(id)
                Case 1 Or 3 Or 5 Or 7 Or 8 Or 10 Or 12
                    ts = 31 - Day(id) + Day(dqsj)
                Case 4 Or 6 Or 9 Or 11
                    ts = 30 - Day(id) + Day(dqsj)
                Case 2
                    nd = Year(id) Mod 4
                    If nd = 0 Then
                        ts = 29 - Day(id) + Day(dqsj)
                    Else
                        ts = 28 - Day(id) + Day(dqsj)
                    End If
            End Select
            ys = (12 - Month(dqsj)) + Month(id) - 1
        End If
    End Ifrq = CStr(ns) & " 年 " & CStr(ys) & " 个月 " & CStr(ts) & " 天 "End Function