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
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
'转换日期格式 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
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
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
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
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
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
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
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
...
endif
然后用错误处理语句
on error goto dateerr
……dateerr:
MsgBox "错误号" & Err.Number & vbCrLf & "错误描述" & Err.Description