/** Jquery擴展方法--by hgx 2018年1月8日-- * 設置時間段函數,開始時間(1號)與結束時間(當前日期) * 傳入參數:--spaceMonth:查詢間隔月,1爲間隔查詢一個月; * --addDay:設置結束時間,0爲當前日期; * --isTime:是否返回日期中帶有時間字符串; * 返回值:arr[0] 開始時間;arr[1] 結束時間 * 調用方法:var arr = $("#TimeStart,#TimeEnd").GetDateStartEndStr(1, -1, true); * 調用方法:var arr = $("#TimeStart,#TimeEnd").SetDateTimeStartEndStr(1, 0); **/ (function ($) { //easyui-datebox $.fn.SetDateStartEndStr = function (spaceMonth, addDay, isTime) { var arr = Array(2); var obj = $(this); //$.parser.parse('#' + obj[0].id); //$.parser.parse('#' + obj[1].id); var dd = new Date(); dd.setDate(dd.getDate() + addDay); var y = dd.getFullYear(); var m = dd.getMonth() + 1; m = (m < 10 ? '0' : '') + m; var d = dd.getDate(); d = (d < 10 ? '0' : '') + d; var enddate = y + "-" + m + "-" + d; arr[1] = enddate; if (isTime) { arr[1] = enddate + " 23:59:59"; } $(obj[1]).datebox("setValue", enddate); $(obj[1]).datebox({ onChange: function (value) { arr[1] = value; if (isTime) { arr[1] = value + " 23:59:59"; } if (value > enddate) { arr[1] = enddate; if (isTime) { arr[1] = enddate + " 23:59:59"; } var obj = $(this); setTimeout(function () { obj.datebox("setValue", enddate); }, 100); } return arr; } }); dd.setMonth(dd.getMonth() + 1 + (spaceMonth * -1)); y = dd.getFullYear(); m = dd.getMonth() + 1; m = (m < 10 ? '0' : '') + m; var startdate = y + "-" + m + "-01"; arr[0] = startdate; if (isTime) { arr[0] = startdate + " 00:00:00"; } $(obj[0]).datebox("setValue", startdate); $(obj[0]).datebox({ onChange: function (value) { arr[0] = value; if (isTime) { arr[0] = value + " 00:00:00"; } return arr; } }); return arr; } //easyui-datetimebox $.fn.SetDateTimeStartEndStr = function (spaceMonth, addDay) { var arr = Array(2); var obj = $(this); //$.parser.parse('#' + obj[0].id); //$.parser.parse('#' + obj[1].id); var dd = new Date(); dd.setDate(dd.getDate() + addDay); var y = dd.getFullYear(); var m = dd.getMonth() + 1; m = (m < 10 ? '0' : '') + m; var d = dd.getDate(); d = (d < 10 ? '0' : '') + d; var enddate = y + "-" + m + "-" + d + " 23:59:59"; arr[1] = enddate; $(obj[1]).datebox("setValue", enddate); $(obj[1]).datebox({ onChange: function (value) { arr[1] = value; if (value > enddate) { arr[1] = enddate; var obj = $(this); setTimeout(function () { obj.datebox("setValue", enddate); }, 100); } return arr; } }); dd.setMonth(dd.getMonth() + 1 + (spaceMonth * -1)); y = dd.getFullYear(); m = dd.getMonth() + 1; m = (m < 10 ? '0' : '') + m; var startdate = y + "-" + m + "-01" + " 00:00:00"; arr[0] = startdate; $(obj[0]).datebox("setValue", startdate); $(obj[0]).datebox({ onChange: function (value) { arr[0] = value; return arr; } }); return arr; } })(jQuery)
調用實例:javascript
<tr> <td style="text-align:right">時間範圍</td> <td> <input id="MTimeStart" class="easyui-datetimebox" value="" style="width:135px" /> <input id="MTimeEnd" class="easyui-datetimebox" value="" style="width:135px" /> </td> </tr>
<script type="text/javascript"> var MTime; $(function () { MTime = $("#MTimeStart,#MTimeEnd").SetDateTimeStartEndStr(-1, 0); console.log(MTime); }); </script>
獲取開始日期: MTime[0]java
獲取結束日期:MTime[1]函數