jQuery datepicker,自定义范围奇怪 [英] jQuery datepicker, custom range quirk

查看:106
本文介绍了jQuery datepicker,自定义范围奇怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery datepicker,并从MySQL数据库收集一组自定义的日期范围。来自查询的结果代码如下:

  //<![CDATA [
$(window) .load(function(){
var ranges = [{start:new Date(2012,02,14),end:new Date(2012,03,31)}];
$(function ){
$(#datepicker)。datepicker({
numberOfMonths:1,
dateFormat:'d MM,yy',
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.length -1] .end
});
});
}); //]]>

(查询的日期范围)。



我的问题是这样的:



当呈现日历时,由于某种原因,它决定将一个月添加到可选择的位置。也就是如果您将结束日期设置为3月31日,则将其更改为5月1日,或者将其设置为!st Feb,将允许您选择至3月1日。我已经尝试从日期减去1个月,但当然这并不适用,因为这几个月有不同的天数,所以如果我有3月31日在那里,我不能简单地减去1个月,因为第31 2月不存在。



任何想法,代码有什么问题?我很确定它的上面的jQuery代码!



你可以在 http://bookingtest.walesdesign.com

解决方案

我认为问题是你正在设置结束日期到4月31日,不存在(所以5月1日退货)。 请记住,从0(1月)到11(12月)的月份。这可以预期从2月14日到28日。

  var ranges = [{start:new Date(2012,1,14),end:new Date(2012,2,28)}]; 
$(function(){
$(#datepicker)datepicker({
numberOfMonths:1,
dateFormat:'d MM,yy',
beforeShowDay:function(date){
for(var i = 0; i< ranges.length; i ++){
if(date> = ranges [i] .start&& = range [i] .end)return [true,''];
}
return [false,''];
},
minDate:ranges [0]。 start,
maxDate:ranges [ranges.length -1] .end
});
});


I am using jQuery datepicker, with a custom set of date ranges collected from a MySQL db. The resulting code from the query looks like this:

//<![CDATA[
$(window).load(function(){
    var ranges = [ { start: new Date(2012, 02, 14), end: new Date(2012, 03, 31) } ];            
    $(function() {    
        $("#datepicker").datepicker({
            numberOfMonths: 1,
            dateFormat: 'd MM, yy',
            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
        });
    });
});//]]> 

(It is the date ranges that are queried).

My problem is this:

When the calendar is rendered, for some reason it decides to add a month onto what is selectable. Ie. if you set the end date as 31st March, it will change it to 1st May, or if you set it as !st Feb, it will allow you to select upto 1st March. I have tried getting around this by subtracting 1 month from the date, but of course this does not work as the months have different numbers of days, so if I had the 31st March in there, I could not simply minus 1 month as the 31st Feb does not exist.

Any ideas what is wrong with the code? I am pretty sure its the jQuery code above!

You can see the example at http://bookingtest.walesdesign.com

解决方案

I think that the problem is that you are setting the end date to 31st of april, which doesn't exists (and so the 1st of May is returned). Remember that months go from 0 (January) to 11 (December). This works as expected from 14 February to 28 march

var ranges = [ { start: new Date(2012, 1, 14), end: new Date(2012, 2, 28) } ];             
$(function() {    
    $("#datepicker").datepicker({
        numberOfMonths: 1,
        dateFormat: 'd MM, yy',
        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
    });
});

这篇关于jQuery datepicker,自定义范围奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆