2010-04-23 140 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

相關問題