在DateRangePicker

时间:2019-07-11 10:04:55

标签: javascript jquery momentjs daterangepicker bootstrap-daterangepicker

我正在使用DateRangePicker作为日期。我试图通过将最小日期设置为当前日期来禁用过去的日期,如下所示。它没有任何作用。

$('.daterange-single').daterangepicker({ 
        singleDatePicker: true,
        minDate: today
    });

但是当您提供如下所示的实际日期时,它可以正常工作。

$('.daterange-single').daterangepicker({ 
        singleDatePicker: true,
        minDate: "7/11/2019"
    });

请帮助我如何将minDate设置为DateRangePicker中的当前日期

3 个答案:

答案 0 :(得分:3)

您可以在daterangepicker中的当前日期设置minDate。您可以尝试以下代码:

$(function() {
  $('input[name="birthday"]').daterangepicker({
    singleDatePicker: true,
    showDropdowns: true,
    minYear: 1901,
    minDate: new Date(),
    maxYear: parseInt(moment().format('YYYY'),10)
  });
});
<script type="text/javascript" src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />

<input type="text" name="birthday" />

答案 1 :(得分:2)

today的值设置为时刻moment()的当前日期

var today = moment()

或简单地指向日期对象

var today = new Date()

$('#datetimepicker').datetimepicker({
  minDate: moment()
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/3.1.3/css/bootstrap-datetimepicker.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/3.1.3/js/bootstrap-datetimepicker.min.js"></script>
<input type="text" id="datetimepicker">

答案 2 :(得分:1)

您可以尝试设置一个变量以包含今天的日期并将其用作minDate

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //As January is 0.
var yyyy = today.getFullYear();
var todayDateString = dd+"/"+mm+"/"+yyyy;

$('.daterange-single').daterangepicker({ 
    singleDatePicker: true,
    minDate: todayDateString
});