I have this code to go on, but what I would like is have these dates being blocked instead of enabled. So just the other way round. Anyone in the know of how to do this based on this code? Thanks a million!
我有这个代码继续,但我想要的是这些日期被阻止而不是启用。所以反过来说。任何知道如何根据此代码执行此操作的人?太感谢了!
var ranges = [ { start: new Date(2010, 8, 1), end: new Date(2010, 8, 20) },
{ start: new Date(2010, 9, 1), end: new Date(2010, 9, 20) },
{ start: new Date(2010, 10, 1), end: new Date(2010, 10, 20) } ];
$(function() {
$("#datepicker").datepicker({
numberOfMonths: 1,
beforeShowDay: function(date) {
for(var i=0; i<ranges.length; i++) {
if(date >= ranges[i].start && date <= ranges[i].end) return [true, ''];
}
return [false, ''];
},
minDate: ranges[0].start,
maxDate: ranges[ranges.length -1].end
});
});
Also, my dateformat is dd-mm-yy. I am a bit puzzled about date formats.
另外,我的dateformat是dd-mm-yy。我对日期格式感到有点困惑。
2
You need to reverse the logic by changing return [true, '']
and return [false, '']
to return [false, '']
and return [true, '']
respectively. You also need to remove minDate: ranges[0].start
and maxDate: ranges[ranges.length -1].end
您需要通过更改return [true,'']并返回[false,'']来返回[false,'']并分别返回[true,'']来反转逻辑。您还需要删除minDate:ranges [0] .start和maxDate:ranges [ranges.length -1] .end
Here's a demo (I've changed the dates slightly to make it easier to see the effect. Just go forward to September 2016).
这是一个演示(我稍微改变了日期,以便更容易看到效果。只需前进到2016年9月)。
var ranges = [ { start: new Date(2016, 8, 1), end: new Date(2016, 8, 20) },
{ start: new Date(2016, 9, 1), end: new Date(2016, 9, 20) },
{ start: new Date(2016, 10, 1), end: new Date(2016, 10, 20) } ];
$(function() {
$("#datepicker").datepicker({
numberOfMonths: 1,
beforeShowDay: function(date) {
for(var i=0; i<ranges.length; i++) {
if(date >= ranges[i].start && date <= ranges[i].end) return [false, ''];
}
return [true, ''];
}
});
});
@import url(https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<input type="text" id="datepicker">
Just a quick edit here to address the date issue. Your date format is a non-standard d(d)-m(m)-yyyy
. You can get it to a standard format pretty easily by converting it to an array, having the values [day,month,year]
as numeric strings (e.g. ["4", "27", "2016"]
for 27-4-2016
). Here's a snippet to do that
只需在此处快速编辑即可解决日期问题。您的日期格式是非标准d(d)-m(m)-yyyy。您可以通过将其转换为数组,将值[日,月,年]作为数字字符串(例如[“4”,“27”,“2016”]为27-4-)轻松地将其转换为标准格式2016)。这是一个片段
// our date-like string and an array equivalent
var d = '27-4-2016',
dArray = d.split('-');
// rearrange the month and day
dArray.splice(1, 0, dArray.shift());
// create a date object called date and pass our array to the constructor
var date = new Date(dArray); // Result: Wed Apr 27 2016 00:00:00 GMT-0500 (CDT)
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2016/04/25/8c5e0c47eb38c36d1c1cbd4beae0c9e5.html。