我现在有一个字符类型的数据"如20041009" 或041009(04年10月09日),怎样判断他是不是有效的日期,如我录入041309就给出出错误提示.

解决方案 »

  1.   

    先分解成标准日期,再用IsDate()判断。
      

  2.   

    Dim aa As String
    aa = "20041009"
    aa = Mid(aa, 1, 4) & "-" & Mid(aa, 5, 2) & "-" & Mid(aa, 7, 2)
    'MsgBox aa
    If IsDate(aa) = True Then
        MsgBox "是有效的日期"
    Else
        MsgBox "不是有效的日期"End If
      

  3.   

    private sub text1_lostfocus()
        if trim(text1.text)<>"" then
            if not isdate(text1.text) then
                msgbox "请输入日期型数据!",48,"提示"
                text1.setfocus
                exit sub
            else
                text1.text=format(text1.text,"YYYY-MM-DD")
            end if
        end if
    end sub
      

  4.   

    '转换日期格式
    cc = Format("04年10月09日", "yyyy/mm/dd")
    对于20041009" 或041009 复杂点
    sRight = "/" & Right("041009", 2)
    sMid = "/" & Mid("041009", Len("041009") - 3, 2)
    sLeft = Mid("041009", 1, Len("041009") - 4)
    cc=format(sRight & sMid & sLeft,"yyyy/mm/dd")
    if isdate(cc)=true then
    ’是日期
    else
    '不是日期
    end if
      

  5.   

    dim str as string
    str="041009"
    if isdate(Right(Left(str, Len(str) - 4), Len(str) - 4) & "-" & Right(Left(str, Len(str) - 2), 2) & "-" & Right(str, 2)) then
    '时间字段
    end if
      

  6.   

    Dim aa As String
    aa = "20041009"if (((Mid(aa, 1, 4)>1000) and (Mid(aa, 1, 4)<9999))   and ((Mid(aa, 5, 2)>0 and (mid(aa, 5, 2)<13)) and  ((Mid(aa, 7, 2)>0) and (Mid(aa, 7, 2)<31) )) =true then
    msgbox"对"
    else
    msgbox"错"
    end if
      

  7.   

    if IsDate(DateVar)=true then
    ...
    endif
      

  8.   

    把SQL Server或Access数据库中的日期字段设置为日期行~
    然后用错误处理语句
    on error goto dateerr
    ……dateerr:
          MsgBox "错误号" & Err.Number & vbCrLf & "错误描述" & Err.Description