日期选择框datetimepicker选择固定月份日期

本人所用时间选择框是基于bootstrap框架,实现功能限制是,后台传入页面一个固定月份,前端只让选择本月份的日期,日期格式为:年、月、日

需要引入的包:

bootstrap-datetimepicker/css/bootstrap-datetimepicker.min.css
bootstrap-datetimepicker/js/locales/bootstrap-datetimepicker.fr.js
bootstrap-datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js

HTML代码:

后台传入的月份隐藏域(格式只包括年、月)


时间框代码:


            


JS代码:

var detailStartTime = $("#period").val() + "-01";
    var detailEndTime;
    var periodY = parseInt($("#period").val().substring(0,3));
    var periodM = parseInt($("#period").val().substring(5));
    if(periodY%4 == 0 && periodM == 2){
        if(periodY%100 == 0 && periodY%400 != 0){
            detailEndTime = $("#period").val() + "-28";
        }else{
            detailEndTime = $("#period").val() + "-29";
        }
    }
    if(periodY%4 != 0 && periodM == 2){
        detailEndTime = $("#period").val() + "-28";
    }
    if(periodM == 1 || periodM == 3 || periodM == 5 || periodM == 7 || periodM == 8 || periodM == 10 || periodM == 12){
        detailEndTime = $("#period").val() + "-31";
    }
    if(periodM == 4 || periodM == 6 || periodM == 9 || periodM == 11){
        detailEndTime = $("#period").val() + "-30";
    }

    var startTime = $("#StartTime");
    var endTime = $("#EndTime");
    startTime.datetimepicker({
        language: 'zh-CN',
        format: 'yyyy-mm-dd',
        autoclose: true,
        todayBtn: true,
        startView: 'month',
        minView: 'month',
        startDate:detailStartTime,
        endDate:detailEndTime
    }).on('changeDate',function () {
        var start = $(this).val();
        var end = endTime.val();
        checkTime(start,end);
    });
    endTime.datetimepicker({
        language: 'zh-CN',
        format: 'yyyy-mm-dd',
        autoclose: true,
        todayBtn: true,
        startView: 'month',
        minView: 'month',
        startDate:detailStartTime,
        endDate:detailEndTime
    }).on('changeDate',function () {
        var start = startTime.val();
        var end = $(this).val();
        checkTime(start,end);
    });

    function checkTime(startTime,endTime) {
        var start = startTime.split('-').join('').toString();
        var end = endTime.split('-').join('').toString();
        if(end && start > end){
            alert("开始时间大于结束时间");
        }
    }


效果图:


日期选择框datetimepicker选择固定月份日期_第1张图片

你可能感兴趣的:(web前端)