开发者

jquery datepicker: select date as back as 6 months

开发者 https://www.devze.com 2022-12-28 12:34 出处:网络
Start date: user can select start date as back as 6 months. example: if today is 04/23/2010 then i can go back up to 11/23/2009 but not more than 6 months.

Start date: user can select start date as back as 6 months. example: if today is 04/23/2010 then i can go back up to 11/23/2009 but not more than 6 months.

<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(); } 
    }); 


}); 

update code:

   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 });

i am getting currentDate = NaN


You can do this with the minDate option, like this:

minDate:'-6m'

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

Your overall code would look like this:

$(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(); } 
  });
}); 


Look into min/maxdate:

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

Note that the month parameter is m-1 (April = 3) for reasons that I don't quite know.

The way I'd do it (simpler to read; might mean the same thing as yours):

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

精彩评论

暂无评论...
验证码 换一张
取 消