Javascript jQuery datepicker minDate 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2484631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery datepicker minDate variable
提问by d3020
I'm usung the jQuery datepicker. In my "EndDate" textbox I'd like to use the date selected from the the "StartDate" textbox + 1. How do I do this?
我正在使用 jQuery 日期选择器。在我的“EndDate”文本框中,我想使用从“StartDate”文本框中选择的日期 + 1。我该怎么做?
I tried this but didn't work. In my start date code I had...
我试过这个,但没有用。在我的开始日期代码中,我有...
test = $(this).datepicker('getDate');
testm = new Date(test.getTime());
testm.setDate(testm.getDate() + 1);
Then in my end date code I had...
然后在我的结束日期代码中我有...
minDate: testm,
but the end date still made all the days for the month available.
但结束日期仍然使该月的所有天数可用。
Edit.I'm curious as to why this doesn't work. In my start date datepicker I have this..
编辑。我很好奇为什么这不起作用。在我的开始日期日期选择器中,我有这个..
onSelect: function (dateText, inst) {
test = dateText
}
Why can't I come down into my end date datepicker and say, minDate: test?
为什么我不能进入我的结束日期日期选择器说,minDate: test?
Edit.Still not working
编辑。还是行不通
$(".dateStartDatePickerBox").datepicker({
minDate:'-0d',
onSelect: function(dateText, inst)
{
test = $(this).datepicker('getDate');
testm = new Date(test.getTime());
testm.setDate(testm.getDate() + 1);
$("#dateEndDatePickerBox").datepicker("option", "minDate", testm);
}
});
$(".dateEndDatePickerBox").datepicker({
onSelect: function()
{
}
});
回答by Joel
You'll need to set the min date dynamically on the change event of the start date.
您需要在开始日期的更改事件上动态设置最小日期。
Something like:
就像是:
$("#startDate").change(function() {
test = $(this).datepicker('getDate');
testm = new Date(test.getTime());
testm.setDate(testm.getDate() + 1);
$("#endDate").datepicker("option", "minDate", testm);
});
Answer to the edit:
回复编辑:
You cannot do the following:
您不能执行以下操作:
var test;
$("#myinput").datepicker({
onSelect: function() { test = $(this).datepicker("getdate"); }
});
$("#myotherinput").datepicker({
minDate: test
});
Test is uninitialized at the time that minDate is being set on myotherinput. The process of setting the minDate doubtless requires DOM manipulation which datepicker manages on initialization or when "option" is called. Simply changing the variable that was used for initialization does not effect the already initialized datepicker.
在设置 minDate 时测试未初始化myotherinput。设置 minDate 的过程无疑需要日期选择器在初始化或调用“选项”时管理的 DOM 操作。简单地更改用于初始化的变量不会影响已经初始化的日期选择器。
回答by Justin Jenkins
Are you talking setting a max selection date?
您是在说设置最大选择日期吗?
StartDate = new Date("March 20, 2010");
EndDate = new Date("March 21, 2010");
$("#datepicker").datepicker({ minDate: StartDate, maxDate: EndDate, defaultDate: StartDate });
Note the maxDate? That won't let you select any days past it ...
注意maxDate?那不会让你选择过去的任何日子......
回答by rfunduk
The API documentationfor the date picker does seem to say it should take a Date object, but I've had this exact problem in the past and this is how I solved it.
日期选择器的 API文档似乎确实说它应该采用 Date 对象,但我过去曾遇到过这个确切的问题,这就是我解决它的方法。
Pass minDatean offset, instead of a date. You'll need a function to take your start date, add one to it and then get the offset from today. So you could have a function like:
传递minDate的偏移,而不是一个日期。你需要一个函数来获取你的开始日期,向它添加一个,然后从今天开始获取偏移量。所以你可以有这样的功能:
function offsetFromToday( someDate ) {
// clear time offset because we only want to take date into account
someDate.setHours( 0 );
someDate.setMinutes( 0 );
// The number of milliseconds in one day
var ONE_DAY = 1000 * 60 * 60 * 24;
// Convert both dates to milliseconds
someDate = someDate.getTime();
var today = new Date().getTime();
// Calculate the difference in milliseconds
var difference = Math.abs( someDate - today );
// Convert back to days and return
return Math.floor( (difference / ONE_DAY) + 1 );
}
So you just need to get your start date a day ahead: StartDate.setDate( StartDate.getDate() + 1 )or similar, and then pass it to this function to get an offset, then give thatto minDate.
所以你只需要提前一天获得你的开始日期:StartDate.setDate( StartDate.getDate() + 1 )或类似的,然后将它传递给这个函数以获得一个偏移量,然后将它提供给minDate.

