如何判断是不是有效的日期

tanghuis 2004-11-02 10:27:39
我现在有一个字符类型的数据"如20041009" 或041009(04年10月09日),怎样判断他是不是有效的日期,如我录入041309就给出出错误提示.
...全文
175 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
江语 2004-11-02
  • 打赏
  • 举报
回复
把SQL Server或Access数据库中的日期字段设置为日期行~
然后用错误处理语句
on error goto dateerr
……

dateerr:
MsgBox "错误号" & Err.Number & vbCrLf & "错误描述" & Err.Description
lxcc 2004-11-02
  • 打赏
  • 举报
回复
if IsDate(DateVar)=true then
...
endif
yjb136 2004-11-02
  • 打赏
  • 举报
回复
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
zyg0 2004-11-02
  • 打赏
  • 举报
回复
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
creazyfish 2004-11-02
  • 打赏
  • 举报
回复
'转换日期格式
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
饮水需思源 2004-11-02
  • 打赏
  • 举报
回复
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
GGL123 2004-11-02
  • 打赏
  • 举报
回复
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
zyg0 2004-11-02
  • 打赏
  • 举报
回复
iddate()
二师兄的老公 2004-11-02
  • 打赏
  • 举报
回复
先分解成标准日期,再用IsDate()判断。

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧