Twitter Bootstrap datepicker:如何仅启用特定的日期范围

我正在开发一些基于Twitter Bootstrap的项目,该项目使用来自https://github.com/eternicode/bootstrap-datepicker的日期选择器(这是其他版本的一个分支),但它缺少一个我需要的非常重要的function -如何仅启用特定日期范围(例如,从过去15天到今天),因此无法选择任何其他日期(不可点击)。

我在SO上找到了一个类似的解决方案,它禁用星期六和星期日: 限制bootstrap-datepicker到工作日? http://jsfiddle.net/katowulf/zNbUT/5/ ,但我不知道如何根据我的需要调整它。

提前致谢。

您链接的类似问题最终使用了很多hack,但您想要的更简单 – 您正在寻找startDateendDate选项:

 $('#dp1').datepicker({ format: 'mm-dd-yyyy', startDate: '-15d', endDate: '+0d' // there's no convenient "right now" notation yet }); 

这些选项可以使用Date对象,timedelta字符串(就像我在这里所做的那样)或者遵循给定format日期字符串。

演示: http : //jsfiddle.net/zNbUT/131/

此外,请确保您使用的是github中的代码 – eyecon.ro的副本是原始代码,包含旧错误,没有新function。

 var startDate = new Date(); startDate.setDate(startDate.getDate()-15); var endDate = new Date(); $('#dp1') .datepicker().on('changeDate', function(ev){ if (ev.date.valueOf() > endDate.valueOf()){ alert('The date selected is too far in the future.'); } else if (ev.date.valueOf() < startDate.valueOf()){ alert('The date selected is too far in the past'); } else { alert('fine') } 

});

http://jsfiddle.net/XSmx6/14/