如何判断时间是否使非法时间

ljmok202 2002-01-08 01:51:34
如何判断一串字符,如“2000-2-30”是非法字符,请给出具体的代码
...全文
128 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljmok202 2002-01-08
  • 打赏
  • 举报
回复
谢谢,
MickeyLi100 2002-01-08
  • 打赏
  • 举报
回复
當然是在javascript中,加入功能函數,先判斷是否閏年,在通過select判斷月份嗎,這樣一步一步的來.
希偌 2002-01-08
  • 打赏
  • 举报
回复
看看下面的吧,其实很简单的!
<%
year2=request("year")
month2=request("month")
date2=request("date")
year1=request("year1")
month1=request("month1")
date1=request("date1")
if cint(year2)>cint(year1) then
response.write "<script>alert('起始时间应该小于终结时间!');history.back();</script>"
elseif cint(year2)=cint(year1) and cint(month2)>cint(month1) then
response.write "<script>alert('起始时间应该小于终结时间!');history.back();</script>"
elseif cint(year2)=cint(year1) and cint(month2)=cint(month1) and cint(date2)>cint(date1) then
response.write "<script>alert('起始时间应该小于终结时间!');history.back();</script>"
end if
if month2="2" then
if (cint(year2) mod 4)<>0 and cint(date2)>28 then
response.write "<script>alert('请选择正确的时间!');history.back();</script>"
elseif (cint(year2) mod 4)=0 and cint(date2)>29 then
response.write "<script>alert('请选择正确的时间!');history.back();</script>"
end if
end if
if month1="2" then
if (cint(year1) mod 4)<>0 and cint(date1)>28 then
response.write "<script>alert('请选择正确的时间!');history.back();</script>"
elseif (cint(year1) mod 4)=0 and cint(date1)>29 then
response.write "<script>alert('请选择正确的时间!');history.back();</script>"
end if
end if
if month2="4" or month2="6" or month2="9" or month2="11" then
if date2="31" then
response.write "<script>alert('请选择正确的时间!');history.back();</script>"
end if
end if
if month1="4" or month1="6" or month1="9" or month1="11" then
if date1="31" then
response.write "<script>alert('请选择正确的时间!');history.back();</script>"
end if
end if
straccess="select * from law where (addtime between CONVERT(datetime(8), '" & year2 & "-" & month2 & "-" & date2 & " 00:00:00') and CONVERT(datetime(8),'" & year1 & "-" & month1 & "-" & date1 & " 00:00:00'))"
set rst=server.createobject("adodb.recordset")
rst.cursortype=1
rst.locktype=3
rst.open straccess,strconn
do while not rst.eof
rst.delete
rst.updatebatch
rst.close
set rst=nothing
rst.movenext
loop
rst.close
set rst=nothing
response.write "<script>alert('成功删除!');history.back();</script>"
%>
ljmok202 2002-01-08
  • 打赏
  • 举报
回复
我的意思是如何判断润年2月,以及4月只有30天等
violet_gj 2002-01-08
  • 打赏
  • 举报
回复
//函数名:chkdate
//功能介绍:检查是否为日期
//参数说明:要检查的字符串
//返回值:0:不是日期 1:是日期
function chkdate(datestr)
{
var lthdatestr
if (datestr != "")
lthdatestr= datestr.length ;
else
lthdatestr=0;

var tmpy="";
var tmpm="";
var tmpd="";
//var datestr;
var status;
status=0;
if ( lthdatestr== 0)
return 0


for (i=0;i<lthdatestr;i++)
{ if (datestr.charAt(i)== '-')
{
status++;
}
if (status>2)
{
//alert("Invalid format of date!");
return 0;
}
if ((status==0) && (datestr.charAt(i)!='-'))
{
tmpy=tmpy+datestr.charAt(i)
}
if ((status==1) && (datestr.charAt(i)!='-'))
{
tmpm=tmpm+datestr.charAt(i)
}
if ((status==2) && (datestr.charAt(i)!='-'))
{
tmpd=tmpd+datestr.charAt(i)
}

}
year=new String (tmpy);
month=new String (tmpm);
day=new String (tmpd)
//tempdate= new String (year+month+day);
//alert(tempdate);
if ((tmpy.length!=4) || (tmpm.length>2) || (tmpd.length>2))
{
//alert("Invalid format of date!");
return 0;
}
if (!((1<=month) && (12>=month) && (31>=day) && (1<=day)) )
{
//alert ("Invalid month or day!");
return 0;
}
if (!((year % 4)==0) && (month==2) && (day==29))
{
//alert ("This is not a leap year!");
return 0;
}
if ((month<=7) && ((month % 2)==0) && (day>=31))
{
//alert ("This month is a small month!");
return 0;

}
if ((month>=8) && ((month % 2)==1) && (day>=31))
{
//alert ("This month is a small month!");
return 0;
}
if ((month==2) && (day==30))
{
//alert("The Febryary never has this day!");
return 0;
}

return 1;
}
tttk 2002-01-08
  • 打赏
  • 举报
回复
对于日期型数据,可以这样判断:
dDate = "2000-2-30"
if IsDate(dDate) then ...

其它类型,请参考VBScript帮助。
希偌 2002-01-08
  • 打赏
  • 举报
回复
var regu = "^(['2000-2-30'])"
var re = new RegExp(regu);
if (theForm.textfile.value.search(re) == -1) {
return true;
} else {
alert ("格式错误");
return false;
}
ttt2 2002-01-08
  • 打赏
  • 举报
回复
isdate()

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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