jQuery DatePicker-日期必须相隔一天,但是如果开始日期是明天,则存在一个错误 [英] jQuery DatePicker - dates have to be one day apart, but there's a bug if the start date is tomorrow
问题描述
我正在使用jQuery DatePicker来确保出发日期至少是到达日期之后的1天.我从
I'm using jQuery DatePicker to ensure that a departure date is at least 1 day after an arrival date. I got the code to do this from this question.
但是,我刚刚注意到,如果我选择明天(2013年5月31日)作为到达日期,那么出发日期将定为2019年1月10日!但是,如果我选择明天以后的到达日期,那似乎可以正常工作.关于这是怎么回事的任何想法?感谢您的帮助.
However, I've just noticed that if I choose tomorrow (31st May 2013) as the arrival date, the departure date gets set to 10th January 2019! But if I pick an arrival date after tomorrow, it seems to work fine. Any ideas on what's going wrong with this? Thanks for any help.
这是 JSFiddle ,其中包含显示问题的代码.
Here's a JSFiddle with the code that shows the issue.
这是JS本身:
$(".datepicker_arrival").datepicker({
dateFormat: 'dd/mm/yy',
minDate: new Date(),
onSelect: function(dateText, inst) {
if($('.datepicker_departure').val() == '') {
var current_date = $.datepicker.parseDate('dd/mm/yy', dateText);
current_date.setDate(current_date.getDate()+1);
$('.datepicker_departure').datepicker('setDate', current_date);
}
},
onClose: function( selectedDate, test) {
var MyDateString = ('0' + (parseInt(test.selectedDay)+1)).slice(-2) + '/'
+ ('0' + (test.selectedMonth+1)).slice(-2) + '/'
+ test.selectedYear;
$( ".datepicker_departure" ).datepicker( "option", "minDate", MyDateString);
}
});
$(".datepicker_departure").datepicker({
dateFormat: 'dd/mm/yy',
minDate: new Date(),
onClose: function( selectedDate ) {
$( ".datepicker_arrival" ).datepicker( "option", "maxDate", selectedDate );
}
});
推荐答案
可以在评论中总结解决方案(这是我提供的,我不仅在寻求答案).
Okay to summarize the solution in the comments (which was provided by me, I'm not just leeching an answer).
该错误是由这段代码引起的:
The error was caused by this piece of code:
var MyDateString = ('0' + (parseInt(test.selectedDay)+1)).slice(-2) + '/'
+ ('0' + (test.selectedMonth+1)).slice(-2) + '/'
+ test.selectedYear;
未在日期中正确添加一天.在添加日期时,没有考虑月份,因此月份的任何结束都会导致创建这样的日期:
which was not correctly adding a day to the date. When adding the day, the months were not taken into account, so any end of the month caused a date like so to be created:
2013年5月32日
32/05/2013
当然没有该月的32号.
Of course there is no 32nd of the month.
因此,可以在此处找到建议的修复代码:
Therefore, a suggested fix code can be found here:
这篇关于jQuery DatePicker-日期必须相隔一天,但是如果开始日期是明天,则存在一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!