【问题标题】:Pick date range in jQuery-UI Datepicker在 jQuery-UI Datepicker 中选择日期范围
【发布时间】:2016-06-24 00:41:34
【问题描述】:
我正在开发一个使用 jQuery-UI datepicker 的网站,问题是当用户使用 a aingle jquery UI datepicker 选择开始日期和结束日期时,我必须选择所有日期并将其放入数组中。
我检查了 jQuery-UI 文档,但没有发现任何可以解决我的问题。
我希望 date-range 像这样被拾取。
我必须使用 jQuery-UI,所以任何有用的想法将不胜感激,
谢谢
【问题讨论】:
标签:
javascript
jquery
css
jquery-ui
datepicker
【解决方案1】:
你可以使用这个方法:
1.用户点击2个日期
2.您将它们保存为两个变量(startDate,endDate)
3.你做一个循环:
var numberOfDaysToAdd=0
var startDate;
var endDate;
var dateCheck=startDate;
var DatetoAddInArray = startDate;
var array = [];
while(DatetoAddInArray!=endDate){
//every time you check if the date is equal to the endDate
//if is not you add it in the array with the dates and then
//you increase the index.
//the while loop ends when you find the end Date
//You can change your code to add or not the start and the end dates
numberOfDaysToAdd++;
DatetoAddInArray.setDate(DatetoAddInArray.getDate()+numberOfDaysToAdd);
array[numberOfDaysToAdd-1]=DatetoAddInArray;
}
上述方法可能是存储从开始日期到结束日期的所有日期的简单方法。
(!) 如果日期选择器允许用户单击 startDate,然后单击 startDate 之前的 endDate,则必须提醒用户选择正确的范围。
谢谢。
【解决方案2】:
我像这样创建了datepicker,所以不需要更多答案。 谢谢
这里工作Fiddle