对于显示的选项,我猜你正在使用eonasdan-datetimepicker。
所以像这样继续:
var maxDate = moment().add(30, 'day');
var $p = jQuery.noConflict();
$p("#foo").datetimepicker({
format: 'YYYY/MM/DD',
useCurrent: true,
showClose: true,
defaultDate: $p.now(),
minDate: moment().startOf('day'),
maxDate: maxDate,
});
maxDate 将使用矩方法 .add() 添加 30 天
对于minDate,如果您使用startOf('day') 会更容易,因为与您使用的相同,但更易于阅读。
编辑
好的,你想要的是让你的用户选择一个不在数组中的“特殊日子”,因此你可以使用enabledDates 选项。
您将在其中添加您的特殊日子和启用的日期范围,因此只有这样才能被选择,并且会是这样的:
let currentFormat = 'YYYY/MM/DD';
let specialDay = '2017/09/10';
let maxDate = moment().add(30, 'day'); //To the picker not allow to go further in the view
let startDate = moment().startOf('day').format(currentFormat); //First date being added, current day
let enabledDates = [
moment(specialDay, currentFormat), //this is our "special day"
moment(startDate, currentFormat) // we format the date to the format needed ];
//Iterate and add 30 days, and only those will be able to be picked
for (var i = 1; i <= 30; i++) {
//apply the format
let date = moment().add(i, 'day').format(currentFormat);
enabledDates.push(moment(date, currentFormat));
}
//We init our picker with the 30 days and our special date
//`minDate` and `maxDate` still there to give the style to the view
$("#myDatepicker").datetimepicker({
format: currentFormat,
useCurrent: false,
showClose: true,
minDate: moment(specialDay, currentFormat),
maxDate: maxDate,
enabledDates: enabledDates,
});
工作小提琴https://jsfiddle.net/William_/2ze7bo27/
编辑:更容易更改格式和特殊日期