datepicker 设置默认日期的方法

1.属性初始化设置

$('#startTime').datepicker({
rtl: App.isRTL(),
orientation: "right",
format: 'yyyy-mm-dd',
language: 'zh-CN',
todayBtn: true,
todayHighlight: true,
autoclose: true ,
viewDate:new Date() //也可以通过function函数获得值
});

2.通过属性设置

$('#startTime').datepicker("setViewDate",new Date());

 3.设置无效

通常都是未放在

    //需要放在工厂函数里面
    $(function() {
         //初始化设置 
        $('#startTime').datepicker({
            rtl : App.isRTL(),
            orientation : "right",
            format : 'yyyy-mm-dd',
            language : 'zh-CN',
            todayBtn : true,
            todayHighlight : true,
            autoclose : true,
            viewDate : new Date(),
            startDate : getDay(0, '-') //开始日期不能小于当天
        });
        $('#endTime').datepicker({
            rtl : App.isRTL(),
            orientation : "right",
            format : 'yyyy-mm-dd',
            language : 'zh-CN',
            todayBtn : true,
            todayHighlight : true,
            autoclose : true
        });
        //文本框初始日期设置,这里间隔为7天
        $('#startTime').val(getDay(0, '-'));
        $('#endTime').val(getDay(7, '-'));
        //正数为往后加日期,负数为昨天、前天。。。,str为日期间隔符,例如 -,/ 
        function getDay(num, str) {
            var today = new Date();
            var nowTime = today.getTime();
            var ms = 24 * 3600 * 1000 * num;
            today.setTime(parseInt(nowTime + ms));
            var oYear = today.getFullYear();
            var oMoth = (today.getMonth() + 1).toString();
            if (oMoth.length <= 1)
                oMoth = '0' + oMoth;
            var oDay = today.getDate().toString();
            if (oDay.length <= 1)
                oDay = '0' + oDay;
            return oYear + str + oMoth + str + oDay;
        }
    })

 

posted on 2019-01-03 10:51  少辉在线  阅读(23347)  评论(0编辑  收藏  举报

导航