2010-04-23 129 views
0

开始日期:用户可以选择开始日期为6个月。例如:如果今天是2010年4月23日,那么我可以回到2009年11月23日但不超过6个月。jquery datepicker:选择日期为6个月

<script type="text/javascript"> 
$(document).ready(function() { 

    $('#endDate').datepicker({ showOn: 'button', 
     buttonImage: '../images/Calendar.png', 
     buttonImageOnly: true, onSelect: function() { }, 
     onClose: function() { $(this).focus(); } 
    }); 


    $('#startDate').datepicker({ showOn: 'button', 
     buttonImage: '../images/Calendar.png', 
     buttonImageOnly: true, onSelect: 
     function (dateText, inst) { 
      $('#endDate').datepicker("option", 'minDate', new Date(dateText)); 
     } 
     , 
     onClose: function() { $(this).focus(); } 
    }); 


}); 

更新代码:

var currentDate = new Date(); 
    var currentMonth = currentDate.getMonth() + 1; 
    var sixMonths = currentMonth + 6 
    currentDate.setMonth(currentDate.currentMonth, sixMonths); 
    var myNewCurrentDate = new Date(currentDate) 
    $('#startDate').datepicker('option', { minDate: myNewCurrentDate }); 

我得到的currentdate = NaN的

回答

4

您可以用minDate option做到这一点,是这样的:

minDate:'-6m' 

Here's an updated sample from a previous question of yours with this in effect

您的总体代码应该是这样的:

$(document).ready(function() { 
    $('#endDate').datepicker({ showOn: 'button', 
     buttonImage: '../images/Calendar.png', 
     buttonImageOnly: true, onSelect: function() { }, 
     onClose: function() { $(this).focus(); } 
    }); 

    $('#startDate').datepicker({ showOn: 'button', 
     buttonImage: '../images/Calendar.png', 
     buttonImageOnly: true, 
     minDate: '-6m', 
     onSelect: function (dateText, inst) { 
      $('#endDate').datepicker("option", 'minDate', new Date(dateText)); 
     } 
     , 
     onClose: function() { $(this).focus(); } 
    }); 
}); 
+0

谢谢尼克.... – 2010-04-24 01:39:29

0

查找到分钟/的maxDate:

$('#startDate').datepicker('option', 'minDate', new Date(2009, 10, 23)); 

注意,本月参数m-1(四月= 3 )因为我不太了解的原因。

我愿意做它的方式(更简单的阅读,可能意味着同样的事情,你的):

var currentDate = new Date(); 
var currentMonth = currentDate.getMonth() + 6 - 1; 
var currentYear = currentDate.getYear(); 
var currentDay = currentDate.getDay(); 
$('#startDate').datepicker('option', 'minDate', new Date(currentYear, currentMonth, currentDay)); 
+0

@Lester:我不想硬编码的日期和我刚才给6个月的例子,是这项工作的minDate“+6”? – 2010-04-23 19:39:26

+0

甚至应该发生什么? beforeShow?或onSelect? – 2010-04-23 19:40:32

+0

嗨。是的,你需要使用var d = new Date; myMonth = d.getMonth()等,然后根据需要操作-6。我不记得是否有一个简单的JavaScript函数,但它很容易谷歌搜索。 HTH! – LesterDove 2010-04-23 19:46:11

相关问题