使用引导程序的datetimepicker从当前日期设置下3个月

时间:2018-10-27 06:27:57

标签: php html twitter-bootstrap datetimepicker

我有两个datetimepicker,该日期被视为“开始日期”,    至今。我正在使用Bootstrap的eonasdan datetimepicker
 我想设置从当前日期至今的下一个3个月。如果我用    maxdate:'+ 90d'和maxdate:'+ 3M',但无法正常工作。在下面的代码段中,我评论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>

2 个答案:

答案 0 :(得分:0)

您需要在Date对象上使用setMonth,然后使用该对象设置日期时间选择器的日期。尝试使用以下代码:

var from = new Date();
from.setMonth(from.getMonth() + 3);
$('.from-date').datetimepicker('date', from);

答案 1 :(得分:0)

简单。只需将maxDate设置为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>