bootstrap 3 datetimepicker开始日期提前2周

时间:2017-08-28 09:24:41

标签: javascript jquery bootstrap-datetimepicker

我想提前2周开始我的datetimepicker ....这里是我的代码

<div class="container">
    <div class="row">
        <div class='col-sm-3'>
            <input type='text' class="form-control" id='datetimepicker4' />
        </div>
    </div>
</div>

<script type="text/javascript">
    $(function () {
        $('#datetimepicker4').datetimepicker();
    });
</script>

3 个答案:

答案 0 :(得分:2)

试试这个代码......

<div class="container">
    <div class="row">
        <div class='col-sm-3'>
            <input type='text' class="form-control" id='datetimepicker4' />
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        $('#datetimepicker4').datetimepicker({
            defaultDate: '+7d'
        });
    });
</script>

答案 1 :(得分:2)

您可以使用解决方案https://jsfiddle.net/0Ltv25o8/5532/

$('#datetimepicker2').datetimepicker({
    defaultDate: moment().add('days', 14)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/a549aa8780dbda16f6cff545aeabc3d71073911e/build/css/bootstrap-datetimepicker.css" rel="stylesheet"/>
<script src="https://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/a549aa8780dbda16f6cff545aeabc3d71073911e/src/js/bootstrap-datetimepicker.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.js"></script>
<div class="row">
    <div class="col-md-12">
      <input type="text" class="form-control" id="datetimepicker2" />
    </div>
</div>

我用了一下时间来增加14天。

您也可以添加周而不是几天

$('#datetimepicker2').datetimepicker({
  defaultDate: moment().add('weeks', 2)
});

禁用过去的日期

$('#datetimepicker2').datetimepicker({
   minDate: moment().add('days', 14)
});

希望这会对你有所帮助。

由于库链接,请参考jsFiddle,因为代码在stackoverflow中不起作用。

答案 2 :(得分:1)

假设您正在使用此库:https://eonasdan.github.io/bootstrap-datetimepicker/

$(function () {
  $('#datetimepicker9').datetimepicker({
    viewMode: 'years',
    defaultDate: formatDate(getDateTwoWeeksFromNow())
  });
});

function getDateTwoWeeksFromNow() {
  new Date(+new Date + 12096e5);
}

function formatDate(date) {
  return [date.getDate(), date.getMonth(),date.getFullYear()].join('/')
}