根據開始時間、結束時間檢索

jsp代碼

 

需要引入的js文件

<script charset="gb2312" type="text/javascript" src="date/WdatePicker.js"></script>
<script  type="text/javascript" src="js/jquery/jquery-1.2.6.min.js"></script>
<script  type="text/javascript" src="js/jquery/checktime.js"></script>

<div class="font_content" align ="center">
   <form name ="search"  method ="post" action="depOrd_listByKeyword.action"  autocomplete="off" onSubmit="return checktime()">
   <table border="0" align="center" width="100%">
    <tr>
    	<td width="10px"></td>
  		<td width="50px">關鍵字:</td><td width="80px"><input type="text" id="keyword" name="keyword"></input></td>
  		<td width="60px">開始時間:</td><td width="80px"><input type="text" id="timeBegin" name="timeBegin" οnfοcus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})"></input></td>
  		<td width="60px">結束時間:</td><td width="80px"><input type="text" id="timeEnd" name="timeEnd" οnfοcus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})"></input></td>
  		<td><input type="submit" value="搜索"  /></td> 
  	</tr>
  	</table>
  	</form>
  </div>


checktime.js文件如下,其實是copy網上別人的代碼

 

function checktime(){
if ($("#timeBegin").length > 0 && $("#timeEnd").length > 0) { 
if (Date.parse(TimeFormatToSQL($("#timeBegin").val()).replace("-", "/")) >
Date.parse(TimeFormatToSQL($("#timeEnd").val()).replace("-", "/"))) { 
{
	alert("開始時間必須小於結束時間!"); 
// $("#txtBeginSearchTimeByBE").focus(); 
	return false; 
}
}
///<summary>
///    將傳遞的時間值轉換爲SQL識別的時間格式
///<param name="strTime">時間(正常的頁面顯示時間格式)</param>
///</summary>
function  TimeFormatToSQL(strTime) {
var strResult = "";
var strTemp = "";
for (var i = 0; i < strTime.length; i++) {
strTemp = strTime.substr(i, 1);
if (strTemp == "年" || strTemp == "月")
    strResult += "-";
else
    if (strTemp == "日" || strTemp == "秒") {
        if (strTemp == "日")
            strResult += "|";
        else
            strResult += "";
    }
    else
        if (strTemp == "時" || strTemp == "分")
            strResult += ":";
        else
            strResult += strTemp;
}
var strArgDateTime = strResult.split('|');  //此時的時間格式可能爲2010-11-11 11: 或2010-11-11 11等格式
if (strArgDateTime.length == 1) {
//日期部分進行處理
var strArgDate = strArgDateTime[0].split('-'); //此時對時間部分進行處理,可能爲11: 11 或11:00等格式
if (strArgDate.length == 2) {
    if (strArgDate[1].length < 1)
        strResult = strArgDate[0];
    else
        strResult = strArgDateTime[0] + "-01";
} else
    if (strArgDate.length == 3) {
        if (strArgDate[2].length < 1)
            strResult = strArgDate[0] + "-" + strArgDate[1] + "-01";
    }
}
else
if (strArgDateTime.length == 2) {
    //時間部分進行處理
    var strArgTime = strArgDateTime[1].split(':'); //此時對時間部分進行處理,可能爲11: 11 或11:00等格式
    if (strArgTime.length == 1) {
        strResult = strArgDateTime[0] + " " + strArgDateTime[1] + ":00:00"
    } else
        if (strArgTime.length == 2) {
            if (strArgTime[1].length < 1)
                strResult = strArgDateTime[0] + " " + strArgDateTime[1] + "00"
            else
                strResult = strArgDateTime[0] + " " + strArgDateTime[1] + ":00"
        } else
            if (strArgTime.length == 3) {
                if (strArgTime[2].length < 1)
                    strResult = strArgDateTime[0] + " " + strArgDateTime[1] + "00"
            }
}
return strResult;
}
}}


 

jquery.js可以在jquery官網下載,而WdatePicker.js也可以在http://www.my97.net/ 官網下載

 

後臺hibernate處理部分代碼,

hql ="from Av a where a.createtime >= (:timeBegin)";

Date timeBegin = getTimeBegin();

Timestamp ts = new Timestamp(timeBegin.getTime()); 
   query.setTimestamp("timeBegin",ts);   


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章