jQuery datetimepicker初始化

/*初始化时间选择框*/
    initDateTimePicker(s, e) {
        jQuery.datetimepicker.setLocale('ch');
        jQuery(function(){
            jQuery(s).datetimepicker({
                format: 'Y-m-d H:i',
                onShow:function( ct ){
                    this.setOptions({
                        maxDate: jQuery(e).val()?jQuery(e).val():new Date(),
                    });
                },
                validateOnBlur:false,
                onChangeDateTime: function(current_time,$input) {
                    var endTime = new Date($(e).val());
                    if(current_time && endTime) {
                        current_time.setSeconds(0);
                        current_time.setMilliseconds(0);
                        if(current_time > new Date() || current_time.getTime() >= endTime.getTime()) {
                            $(s).val("");
                            Messenger().post({
                                message: '开始时间大于当前时间或结束时间,请重新选择!',
                                type: 'error',
                                showCloseButton: true,
                                hideAfter: 2
                            });
                            Messenger({
                                extraClasses: 'messenger-fixed messenger-on-top'
                            });
                        }
                    }  
                }
            });
            jQuery(e).datetimepicker({
                format: 'Y-m-d H:i',
                onShow:function( ct ){
                    this.setOptions({
                        minDate: jQuery(s).val()?jQuery(s).val():false,
                        maxDate: new Date()
                    });
                },
                validateOnBlur:false,
                onChangeDateTime: function(current_time,$input) {
                    var startTime = new Date($(s).val());
                    if(current_time && startTime) {
                        current_time.setSeconds(0);
                        current_time.setMilliseconds(0);
                        if(current_time > new Date() || current_time.getTime() <= startTime.getTime()) {
                            $(e).val("");
                            Messenger().post({
                                message: '结束时间大于当前时间或小于开始时间,请重新选择!',
                                type: 'error',
                                showCloseButton: true,
                                hideAfter: 2
                            });
                            Messenger({
                                extraClasses: 'messenger-fixed messenger-on-top'
                            });
                        }
                    } 
                }
            });
        });
    }


 
  

使用:

initDateTimePicker("#start-time-info", "#end-time-info");


https://github.com/xdan/datetimepicker

你可能感兴趣的:(javascript)