bootstrap金沙官网线上-daterangepicker插件运用

引入:daterangepicker.css、daterangepicker.js、moment.js、moment.min.js

显示具体时间时分秒:

链接:https://files.cnblogs.com/files/kitty-blog/moment.min.js、https://files.cnblogs.com/files/kitty-blog/moment.js、https://files.cnblogs.com/files/kitty-blog/daterangepicker.js、https://files.cnblogs.com/files/kitty-blog/daterangepicker.css

timePicker设置为true,//有些资料写的pickerTime不太对

/**初始化 Daterangepicker 插件 */
function initDaterangepicker() {
    $('.daterangepicker').daterangepicker({
        "showDropdowns": true,
        "showWeekNumbers": false,
        "showISOWeekNumbers": false,
        "timePicker": true,
        "timePicker24Hour": true,
        "timePickerSeconds": false,
        "autoApply": false,
        "locale": {
            "direction": "ltr",
            "format": "YYYY-MM-DD HH:mm",
            "separator": " 至 ",
            "applyLabel": "确定",
            "cancelLabel": "取消",
            "fromLabel": "From",
            "toLabel": "To",
            "daysOfWeek": [
                "周六",
                "周一",
                "周二",
                "周三",
                "周四",
                "周五",
                "周日"
            ],
            "monthNames": [
                "一月",
                "二月",
                "三月",
                "四月",
                "五月",
                "六月",
                "七月",
                "八月",
                "九月",
                "十月",
                "十一月",
                "十二月"
            ],
            "firstDay": 1
        },
        "alwaysShowCalendars": false,
        "parentEl": "daterangepicker",
        "startDate": moment(),
        "endDate": moment(),
        "minDate": false,
        "maxDate": "05/28/2050",
        "applyClass": "btn-green btn-outline",
        "cancelClass": "btn-default btn-outline",
        "opens": "center",
        "drops": "down"
    }, function (start, end, label) {
        console.log("New date range selected: " + start.format('YYYY-MM-DD HH:mm') + " to " + end.format('YYYY-MM-DD HH:mm') + " (predefined range: " + label + ")");
        $("#startTime").val(start.format('YYYY-MM-DD HH:mm'));
        $("#endTime").val(end.format('YYYY-MM-DD HH:mm'));
    });
}
/**初始化 datetimepicker 日期 插件*/
function initDatepicker(defaultStartDate, defaultEndDate) {
    var now = new Date();
    //date + time
    var picker1 = $('.datetimepicker-startdate').datetimepicker({
        format: 'YYYY-MM-DD',
        locale: moment.locale('zh-cn'),
        defaultDate: defaultStartDate,
        minDate: false,
        maxDate: false,
        ignoreReadonly: true,
        allowInputToggle: true,
        icons: {
            time: 'fa fa-clock-o',
            date: 'fa fa-calendar',
            up: 'fa fa-chevron-up',
            down: 'fa fa-chevron-down',
            previous: 'fa fa-chevron-left',
            next: 'fa fa-chevron-right',
            today: 'fa fa-crosshairs',
            clear: 'fa fa-trash'
        }
    });
    var picker2 = $('.datetimepicker-enddate').datetimepicker({
        format: 'YYYY-MM-DD',
        locale: moment.locale('zh-cn'),
        defaultDate: defaultEndDate,
        minDate: defaultStartDate,
        maxDate: false,
        ignoreReadonly: true,
        allowInputToggle: true,
        icons: {
            time: 'fa fa-clock-o',
            date: 'fa fa-calendar',
            up: 'fa fa-chevron-up',
            down: 'fa fa-chevron-down',
            previous: 'fa fa-chevron-left',
            next: 'fa fa-chevron-right',
            today: 'fa fa-crosshairs',
            clear: 'fa fa-trash'
        }
    });
    //动态设置最小值  
    picker1.on('dp.change', function (e) {
        picker2.data('DateTimePicker').minDate(e.date);
    });
    //动态设置最大值  
    picker2.on('dp.change', function (e) {
        picker1.data('DateTimePicker').maxDate(e.date);
    });
}
/**初始化 datetimepicker 时间 插件*/
function initTimepicker(defaultStartTime, defaultEndTime) {
    var now = new Date();
    // only time
    var picker1 = $('.datetimepicker-starttime').datetimepicker({
        format: 'LT',
        locale: moment.locale('zh-cn'),
        defaultDate: defaultStartTime == "" ? 5 : defaultStartTime,
        minDate: false,
        maxDate: false,
        ignoreReadonly: true,
        allowInputToggle: true,
    });
    var picker2 = $('.datetimepicker-endtime').datetimepicker({
        format: 'LT',
        locale: moment.locale('zh-cn'),
        defaultDate: defaultEndTime == "" ? 21 : defaultEndTime,
        minDate: defaultStartTime,
        maxDate: false,
        ignoreReadonly: true,
        allowInputToggle: true,
    });
    //动态设置最小值  
    picker1.on('dp.change', function (e) {
        picker2.data('DateTimePicker').minDate(e.date);
    });
    //动态设置最大值  
    picker2.on('dp.change', function (e) {
        picker1.data('DateTimePicker').maxDate(e.date);
    });
}

重点大坑:金沙官网线上,修改时间默认展示格式,把fomat写在locale中,网上很多资料说直接写在daterangepicker属性中,这样是不生效的。

 

起止时间可以设置为具体年月日也可以生成当前日期(new Date()  或者 moment()【moment()方法为moment.js获取当前时间的函数】)

本文由金沙官网线上发布于Web前端,转载请注明出处:bootstrap金沙官网线上-daterangepicker插件运用

您可能还会对下面的文章感兴趣: