doushan3511 2018-10-27 06:27
浏览 207
已采纳

使用bootstrap的datetimepicker从当前日期开始设置接下来的3个月

I have two datetimepicker for a date which is considered as From Date & To date. I am using eonasdan datetimepicker of Bootstrap.
I want to set the next 3 month from current date to date. If I use maxdate:'+90d' and maxdate:'+3M',but it is not work properly. In below snippet I comment maxDate:

<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.min.css" />

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>
<script>
  function share() {
    $("#myModal").modal('show');
  }
</script>
<div class="container">
  <div class="row">
    <button type="button" name="share_video" class="btn btn-default" onclick="share()">Share</button>
    <div class="modal fade" id="myModal" role="dialog">
      <div class="modal-dialog modal-md">

        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <center>
              <h4 class="modal-title">Share</h4>
            </center>

          </div>
          <div class="modal-body">
            <form action="#" method="post" style="padding: 1px 30px !important;">


              <div class="form-group">
                <label class="col-md-12">Duration</label>
                <div class="col-md-12">
                  <label class="col-md-12">From</label>
                  <div class="col-md-6" id=''>
                    <input type="text" name="FromDate" id="FromDate" class="form-control" name="setTodaysDate" placeholder="MM/DD/YYYY"></div>

                  <div class="col-md-6">
                    <input type="text" name="TimeTO" class="form-control col-md-12" id="TimeFrom" placeholder="HH:MM"></div>
                </div>
                <div class="col-md-12">
                  <label class="col-md-12">To</label>
                  <div class="col-md-6" id=''>
                    <input type="text" id="ToDate" class="form-control" name="setTodaysDate" placeholder="MM/DD/YYYY"></div>
                  <div class="col-md-6">
                    <input type="text" name="TimeFrom" class="form-control col-md-12" id="TimeTO" placeholder="HH:MM"></div>
                </div>
              </div>
              <div class="row form-group col-md-12"></div>

              <div class="form-group">
                <center><button type="submit" name="submit" class="btn btn-default" style="border:none;color:#FFFFFF; background:#2DADBA;">Submit</button></center>
              </div>

            </form>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<script type="text/javascript">
  $(function() {


    $('#FromDate,#ToDate').datetimepicker({
      format: 'L',
      format: 'YYYY-MM-DD',
      useCurrent: true,
      minDate: moment(),
      //maxdate:'+3M'
      //maxdate:'+90d'
    });
    $('#FromDate').datetimepicker().on('dp.change', function(e) {
      var incrementDay = moment(new Date(e.date));
      incrementDay.add(1, 'days');
      $('#ToDate').data('DateTimePicker').minDate(incrementDay);
      $(this).data("DateTimePicker").hide();
    });

    $('#ToDate').datetimepicker().on('dp.change', function(e) {
      var decrementDay = moment(new Date(e.date));
      decrementDay.subtract(1, 'days');
      $('#FromDate').data('DateTimePicker').maxDate(decrementDay);
      $(this).data("DateTimePicker").hide();
    });

  });
</script>

</div>
  • 写回答

2条回答 默认 最新

  • dongqi8030 2018-10-27 07:00
    关注

    Simple. Just set the maxDate to moment.add(3, 'months')

    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.min.css" />
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>
    <script>
      function share() {
        $("#myModal").modal('show');
      }
    </script>
    <div class="container">
      <div class="row">
        <button type="button" name="share_video" class="btn btn-default" onclick="share()">Share</button>
        <div class="modal fade" id="myModal" role="dialog">
          <div class="modal-dialog modal-md">
    
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <center>
                  <h4 class="modal-title">Share</h4>
                </center>
    
              </div>
              <div class="modal-body">
                <form action="#" method="post" style="padding: 1px 30px !important;">
    
    
                  <div class="form-group">
                    <label class="col-md-12">Duration</label>
                    <div class="col-md-12">
                      <label class="col-md-12">From</label>
                      <div class="col-md-6" id=''>
                        <input type="text" name="FromDate" id="FromDate" class="form-control" name="setTodaysDate" placeholder="MM/DD/YYYY"></div>
    
                      <div class="col-md-6">
                        <input type="text" name="TimeTO" class="form-control col-md-12" id="TimeFrom" placeholder="HH:MM"></div>
                    </div>
                    <div class="col-md-12">
                      <label class="col-md-12">To</label>
                      <div class="col-md-6" id=''>
                        <input type="text" id="ToDate" class="form-control" name="setTodaysDate" placeholder="MM/DD/YYYY"></div>
                      <div class="col-md-6">
                        <input type="text" name="TimeFrom" class="form-control col-md-12" id="TimeTO" placeholder="HH:MM"></div>
                    </div>
                  </div>
                  <div class="row form-group col-md-12"></div>
    
                  <div class="form-group">
                    <center><button type="submit" name="submit" class="btn btn-default" style="border:none;color:#FFFFFF; background:#2DADBA;">Submit</button></center>
                  </div>
    
                </form>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <script type="text/javascript">
      $(function() {
    
    
        $('#FromDate,#ToDate').datetimepicker({
          format: 'L',
          format: 'YYYY-MM-DD',
          useCurrent: true,
          minDate: moment(),
          maxDate: moment().add(3, 'months')
        });
        $('#FromDate').datetimepicker().on('dp.change', function(e) {
          var incrementDay = moment(new Date(e.date));
          incrementDay.add(1, 'days');
          $('#ToDate').data('DateTimePicker').minDate(incrementDay);
          $(this).data("DateTimePicker").hide();
        });
    
        $('#ToDate').datetimepicker().on('dp.change', function(e) {
          var decrementDay = moment(new Date(e.date));
          decrementDay.subtract(1, 'days');
          $('#FromDate').data('DateTimePicker').maxDate(decrementDay);
          $(this).data("DateTimePicker").hide();
        });
    
      });
    </script>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀