Итак, у меня есть средство выбора даты, которое позволяет выбирать только 2 даты в месяц, 1-ю и 15-ю. Проблема в том, что он по-прежнему позволяет выбирать предыдущие даты. Например, если это 1 октября, нельзя выбрать все предыдущие даты. Как сделать так, чтобы все предыдущие даты нельзя было выбрать?

<link href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<input id="side-datepicker">

$('#side-datepicker').datepicker({
     beforeShowDay: function (date) {
    //getDate() returns the day (0-31)
     if (date.getDate() == 1 || date.getDate() == 15) {
     return [true, ''];
    }
    return [false, ''];
    },


    });
2
brandbei37 25 Окт 2019 в 15:38
Какую библиотеку вы используете? .datepicker не является функцией jQuery ...
 – 
Rojo
25 Окт 2019 в 15:46
Извините, только что добавил библиотеку
 – 
brandbei37
25 Окт 2019 в 15:49
Также добавьте его как тег :)
 – 
Rojo
25 Окт 2019 в 15:50
Как выбрать второе свидание?
 – 
Mischa
25 Окт 2019 в 15:53
Вы этого не сделаете, это позволяет только 2 даты. Таким образом, можно выбрать только две даты.
 – 
brandbei37
25 Окт 2019 в 15:55

2 ответа

$('#side-datepicker').datepicker({
  beforeShowDay: function (date) {
    let today = new Date('2019-10-24');
    //if you want the current date to be selectable uncomment the following line
    //today.setDate(today.getDate() -1);
    if (date > today && (date.getDate() == 1 || date.getDate() == 15)) {
      return [true, ''];
    }
    return [false, ''];
  }
});
<link href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<input id="side-datepicker">
0
Mischa 25 Окт 2019 в 16:33

Вы захотите использовать параметры минимальной даты, чтобы установить нижнюю часть окна выбора даты, например:

var currentTime = new Date() 
var minDate = new Date(currentTime.getFullYear(), currentTime.getMonth(), +1); //first day of current month
$('#side-datepicker').datepicker({
     minDate: minDate, 
     beforeShowDay: function (date) {

    //getDate() returns the day (0-31)
     if (date.getDate() == 1 || date.getDate() == 15) {
     return [true, ''];
    }
    return [false, ''];
    },


    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<input id="side-datepicker">

Если вы хотите ограничить его только текущим месяцем и днями, которые не прошли. Пример разрешает только 1-е и 15-е, но сегодня 25 октября (после 1-го и 15-го), поэтому первой доступной датой для выбора будет 1 ноября, тогда вы измените свою минимальную дату следующим образом:

var minDate = new Date(currentTime.getFullYear(), currentTime.getMonth(), currentTime.getDate()); //current date

Пример:

var currentTime = new Date() 
var minDate = new Date(currentTime.getFullYear(), currentTime.getMonth(), currentTime.getDate()); //current date
$('#side-datepicker').datepicker({
     minDate: minDate, 
     beforeShowDay: function (date) {

    //getDate() returns the day (0-31)
     if (date.getDate() == 1 || date.getDate() == 15) {
     return [true, ''];
    }
    return [false, ''];
    },


    });
<link href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<input id="side-datepicker">
0
Travis Acton 25 Окт 2019 в 16:55