利用JavaScript实现时间段的查询
时间:2007-02-17 来源:PHP爱好者
以后,要是做时间段的查询就方便了,不过就是程序有点多,呵呵!
希望大家能帮我测试一下,输入的时间格式是1999-01-01或2000-10-01或2000-10-10,即月份和时间必须是两位,为了方便大家测试,把所有文件放在这里,只要运行time_main.asp就可以了,多谢了!
time_main.asp
<%@ Language=VBScript %>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<FRAMESET ROWS="82,18" border=1>
<FRAME NAME="time" SRC="time.asp">
<FRAME NAME="time_search" SRC="time_search.asp">
</FRAMESET>
<NOFRAMES>您的浏览器不支持FRAMES</NOFRAMES>
</HTML>
time.asp
<%@ Language=VBScript %>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
<%
starttime = Request.Form("starttime")
endtime = Request.Form("endtime")
Response.Write starttime&"<br>"
Response.Write endtime&"<br>"
%>
请点击“查询”按钮
</BODY>
</HTML>
time_search.asp
<%@ Language=VBScript %>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
<center>
<form name="tform" method="post">
按时间(以<font color=red>[YYYY-MM-DD]</font>格式输入):
<input type="text" name="starttime" size=10 maxlength=10 value="1999-01-02">到
<input type="text" name="endtime" size=10 maxlength=10 value="2000-01-01">
<input type="button" name="tsearch" value="查 询" onclick="return tcheck()">
</form>
</center>
<script language="javascript">
<!--
function tcheck()
{
if (window.document.tform.starttime.value != "")
{
var vDate,vYear,vMonth,vDay;
vDate = window.document.tform.starttime.value;
vYear = vDate.substring(0,4);
if (isNaN(vYear) || vYear <1900)
{
window.alert("您的日期1输入有误!");
tform.starttime.focus();
return false;
}
vMonth = vDate.substring(5,7);
if (isNaN(vMonth) || vMonth <01 || vMonth > 12)
{
window.alert("您的日期1输入有误!");
tform.starttime.focus();
return false;
}
vDay = vDate.substring(8,10);
if (isNaN(vDay) || vDay == "" || vDay.length != 2 || vDay <01 || vDay > 31)
{
window.alert("您的日期1输入有误!");
tform.starttime.focus();
return false;
}
if (vMonth == 04 || vMonth == 06 || vMonth == 09 || vMonth == 11)
{
if (vDay > 30)
{
window.alert("您的日期1输入有误!");
return false;
}
}
if (vMonth == 02)
{
if (vYear % 4 == 0 && vYear % 100 || vYear % 400 == 0)
{
if (vDay > 29)
{
window.alert("您的日期1输入有误!");
tform.starttime.focus();
return false;
}
}
else
{
if (vDay > 28)
{
window.alert("您的日期1输入有误!");
tform.starttime.focus();
return false;
}
}
}
if (vDate.substring(4,5) !== "-" || vDate.substring(7,8) != "-")
{
window.alert("您的输入有误,请以YYYY-MM-DD格式输入!");
tform.starttime.focus();
return false;
}
if (window.document.tform.starttime.value != "" && window.document.tform.endt
ime.value != "")
{
if (window.document.tform.starttime.value > window.document.tform.endtime.va
lue)
{
window.alert("起始日期不能大于终止日期!");
tform.starttime.focus();
return false;
}
}
}
if (window.document.tform.endtime.value != "")
{
var vDate,vYear,vMonth,vDay;
vDate = window.document.tform.endtime.value;
vYear = vDate.substring(0,4);
if (isNaN(vYear))
{
window.alert("您的日期2输入有误!");
tform.endtime.focus();
return false;
}
vMonth = vDate.substring(5,7);
if (isNaN(vMonth) || vMonth <01 || vMonth > 12)
{
window.alert("您的日期2输入有误!");
tform.endtime.focus();
return false;
}
vDay = vDate.substring(8,10);
if (isNaN(vDay) || vDay == "" || vDay.length != 2 || vDay <01 || vDay > 31)
{
window.alert("您的日期2输入有误!");
tform.endtime.focus();
return false;
}
if (vMonth == 04 || vMonth == 06 || vMonth == 09 || vMonth == 11)
{
if (vDay > 30)
{
window.alert("您的日期2输入有误!");
return false;
}
}
if (vMonth == 02)
{
if (vYear % 4 == 0 && vYear % 100 == 0 || vYear % 400 == 0)
{
if (vDay > 29)
{
window.alert("您的日期2输入有误!");
tform.endtime.focus();
return false;
}
}
else
{
if (vDay > 28)
{
window.alert("您的日期2输入有误!");
tform.endtime.focus();
return false;
}
}
}
if (vDate.substring(4,5) !== "-" || vDate.substring(7,8) != "-")
{
window.alert("您的输入有误,请以YYYY-MM-DD格式输入!");
tform.endtime.focus();
return false;
}
}
//如果必须按时间查询的话,就把注释放开就可以了
//if (window.document.tform.starttime.value == "" && window.document.tform.end
time.value == "")
//{
// window.alert("对不起,请你输入时间!");
// window.document.tform.starttime.focus();
// return false;
//}
window.document.tform.action = "time.asp";
window.document.tform.target = "time";
window.document.tform.submit();
}
//-->
</script>
</BODY>
</HTML>
php爱好者站 http://www.phpfans.net c/vc/c++/java.