我有两个日期选择器,用于选择日期范围。以下是他们的代码:
$("#ContentPlaceHolder1_MySearchBox_txtOutDate").datepicker({
changeMonth: true,
numberOfMonths: 1,
dateFormat: 'dd-M-yy',
onClose: function (selectedDate) {
$("#ContentPlaceHolder1_MySearchBox_txtInDate").datepicker("option", "minDate", selectedDate);
DoPostBack();
},
beforeShow: function () {
setTimeout(function () {
$('.ui-datepicker').css('z-index', 99999999999999);
}, 0);
}
}).val();
$("#ContentPlaceHolder1_MySearchBox_txtInDate").datepicker({
changeMonth: true,
numberOfMonths: 1,
dateFormat: 'dd-M-yy',
onClose: function (selectedDate) {
$("#ContentPlaceHolder1_MySearchBox_txtOutDate").datepicker("option", "maxDate", selectedDate);
DoPostBack();
},
beforeShow: function () {
setTimeout(function () {
$('.ui-datepicker').css('z-index', 99999999999999);
}, 0);
}
}).val();
当不允许InDate低于OutDate时,代码工作正常。但我需要将InDate的最小值设置为OutDate后至少3天。我尝试使用New Date(selectedDate)
添加3天,但由于“selectedDate”是dd-MMM-YYYY格式的字符串,因此无法转换为日期。
实现目标的最简单方法是什么?
由于
答案 0 :(得分:0)
行。我找到了一个使用以下功能的解决方案:
function parseDate(input) {
var parts = input.split('-');
return new Date(parts[2], MonthNameToNumber(parts[1]) - 1, parts[0]);
}
function MonthNameToNumber(MonthName) {
switch(MonthName) {
case "Jan":
return 1;
break;
case "Feb":
return 2;
break;
case "Mar":
return 3;
break;
case "Apr":
return 4;
break;
case "May":
return 5;
break;
case "Jun":
return 6;
break;
case "Jul":
return 7;
break;
case "Aug":
return 8;
break;
case "Sep":
return 9;
break;
case "Oct":
return 10;
break;
case "Nov":
return 11;
break;
case "Dec":
return 12;
break;
default:
return 1;
}
}
然后改变:
$("#ContentPlaceHolder1_MySearchBox_txtInDate").datepicker("option", "minDate", selectedDate);
到
var OutDate = parseDate(selectedDate);
OutDate.setDate(OutDate.getDate() + 3);
$("#<%=txtInDate.ClientID%>").datepicker("option", "minDate", OutDate);
和
$("#ContentPlaceHolder1_MySearchBox_txtOutDate").datepicker("option", "maxDate", selectedDate);
到
var InDate = parseDate(selectedDate);
InDate.setDate(InDate.getDate() - 3);
$("#<%=txtOutDate.ClientID%>").datepicker("option", "maxDate", InDate);