EasyUI--datebox设置默认时间

时间:2023-06-29 16:08:08

1. html代码:

<input id="txtBeginTime" class="easyui-datebox" data-options="formatter:myformatter,parser:myparser"></input> <b>--</b>
<input id="txtEndTime" class="easyui-datebox" data-options="formatter:myformatter,parser:myparser"></input>

2. js代码:

function myformatter(date){
            var y = date.getFullYear();
            var m = date.getMonth()+1;
            var d = date.getDate();
            return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
        }
       
        function myparser(s){
            if (!s) return new Date();
            var ss = (s.split('-'));
            var y = parseInt(ss[0],10);
            var m = parseInt(ss[1],10);
            var d = parseInt(ss[2],10);
            if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
                return new Date(y,m-1,d);
            } else {
                return new Date();
            }
        }

//页面加载

   $(function(){

//设置时间
       var curr_time = new Date();   
       $("#txtBeginTime").datebox("setValue",myformatter(curr_time));
       $("#txtEndTime").datebox("setValue",myformatter(curr_time));
       //获取时间
       var beginTime=$("#txtBeginTime").datebox("getValue");
       var endTime=$("#txtEndTime").datebox("getValue");

});