当前位置: 移动技术网 > IT编程>开发语言>JavaScript > bootstrap daterangepicker双日历时间段选择控件详解

bootstrap daterangepicker双日历时间段选择控件详解

2017年12月12日  | 移动技术网IT编程  | 我要评论

双日历时间段选择插件 — daterangepicker是bootstrap框架后期的一个时间控件,可以设定多个时间段选项,也可以自定义时间段,由用户自己选择起始时间和终止时间,时间段的最大跨度可以在程序里设定。我们项目里用到的bootstrap版本是2.3.1,所以我把daterangepicker与bootstrap-2.3.1进行了整合。  

一、需要引入的css与js 

<link href="bootstrap.min.css" rel="stylesheet">   
<link rel="stylesheet" type="text/css" media="all" href="daterangepicker-bs3.css" /> 
<link rel="stylesheet" type="text/css" media="all" href="daterangepicker-1.3.7.css" /> 
<link href="font-awesome-4.1.0/css/font-awesome.min.css" rel="stylesheet"> 
 
<script type="text/javascript" src="jquery-1.10.1.min.js"></script> 
<script type="text/javascript" src="bootstrap.min.js"></script> 
<script type="text/javascript" src="moment.js"></script> 
<script type="text/javascript" src="daterangepicker-1.3.7.js"></script> 

 二、html部分代码

<div class="page-content"> 
      <!-- begin page container--> 
  <div class="container-fluid"> 
    <div class="row-fluid" style="margin-top:5px"> 
      <div class="span4"> 
        <div class="control-group"> 
          <label class="control-label"> 
            日期: 
          </label> 
        <div class="controls"> 
          <div id="reportrange" class="pull-left daterange" style="width:350px"> 
            <i class="glyphicon glyphicon-calendar fa fa-calendar"></i> 
            <span id="searchdaterange"></span> 
            <b class="caret"></b> 
          </div> 
        </div> 
      </div> 
    </div> 
  </div> 
</div> 

三、使用js调用daterangepicker 

<script type="text/javascript"> 
    $(document).ready(function (){ 
          //时间插件 
          $('#reportrange span').html(moment().subtract('hours', 1).format('yyyy-mm-dd hh:mm:ss') + ' - ' + moment().format('yyyy-mm-dd hh:mm:ss')); 
     
          $('#reportrange').daterangepicker( 
              { 
                // startdate: moment().startof('day'), 
                //enddate: moment(), 
                //mindate: '01/01/2012',  //最小时间 
                maxdate : moment(), //最大时间  
                datelimit : { 
                  days : 30 
                }, //起止时间的最大间隔 
                showdropdowns : true, 
                showweeknumbers : false, //是否显示第几周 
                timepicker : true, //是否显示小时和分钟 
                timepickerincrement : 60, //时间的增量,单位为分钟 
                timepicker12hour : false, //是否使用12小时制来显示时间 
                ranges : { 
                  //'最近1小时': [moment().subtract('hours',1), moment()], 
                  '今日': [moment().startof('day'), moment()], 
                  '昨日': [moment().subtract('days', 1).startof('day'), moment().subtract('days', 1).endof('day')], 
                  '最近7日': [moment().subtract('days', 6), moment()], 
                  '最近30日': [moment().subtract('days', 29), moment()] 
                }, 
                opens : 'right', //日期选择框的弹出位置 
                buttonclasses : [ 'btn btn-default' ], 
                applyclass : 'btn-small btn-primary blue', 
                cancelclass : 'btn-small', 
                format : 'yyyy-mm-dd hh:mm:ss', //控件中from和to 显示的日期格式 
                separator : ' to ', 
                locale : { 
                  applylabel : '确定', 
                  cancellabel : '取消', 
                  fromlabel : '起始时间', 
                  tolabel : '结束时间', 
                  customrangelabel : '自定义', 
                  daysofweek : [ '日', '一', '二', '三', '四', '五', '六' ], 
                  monthnames : [ '一月', '二月', '三月', '四月', '五月', '六月', 
                      '七月', '八月', '九月', '十月', '十一月', '十二月' ], 
                  firstday : 1 
                } 
              }, function(start, end, label) {//格式化日期显示框 
                 
                $('#reportrange span').html(start.format('yyyy-mm-dd hh:mm:ss') + ' - ' + end.format('yyyy-mm-dd hh:mm:ss')); 
              }); 
 
      //设置日期菜单被选项 --开始-- 
     /* 
         var dateoption ; 
         if("${riqi}"=='day') { 
            dateoption = "今日"; 
         }else if("${riqi}"=='yday') { 
            dateoption = "昨日"; 
         }else if("${riqi}"=='week'){ 
            dateoption ="最近7日"; 
         }else if("${riqi}"=='month'){ 
            dateoption ="最近30日"; 
         }else if("${riqi}"=='year'){ 
            dateoption ="最近一年"; 
         }else{ 
            dateoption = "自定义"; 
         } 
          $(".daterangepicker").find("li").each(function (){ 
            if($(this).hasclass("active")){ 
              $(this).removeclass("active"); 
            } 
            if(dateoption==$(this).html()){ 
              $(this).addclass("active"); 
            } 
         });*/ 
            //设置日期菜单被选项 --结束-- 
    }) 
</script> 

 四、效果图

五、实例下载地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网