views:

207

answers:

2

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

A: 

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));
LesterDove
@Lester: i dont want to hardcode the date and i have just give the example of 6 months, is that work minDate "+6" ?
Abu Hamzah
also what even that should be occure?beforeShow ? or onSelect ?
Abu Hamzah
Hi. Yes, you'll need to use var d = new Date; myMonth= d.getMonth(), etc, and then manipulate the -6 as needed. I don't remember offhand whether there's an easy JavaScript function, but it's easily Googled. HTH!
LesterDove
To your second question, you can add this as a separate line of code any time after you've invoked the datepicker. (For instance, just before the final closing brackets of your example post.) You need not jam it into the original definition, although you can if you want.
LesterDove
see my update code, i am getting NaN
Abu Hamzah
made a quick edit
LesterDove
when the calendar loads i am getting by default month of september but what i want is, when i click on the calendar i just get the current month but when i click on previous month, i should be able to go all the way upto nov 2009, i think you misunderstood my requirement, i have very clearly say in the very first post with example.
Abu Hamzah
hi, i understand the requirement, you definitely want to use the minDate option -- i just can't tell why it's not working. i'll do a mockup later tonight if you don't mind the delay. good luck otherwise.
LesterDove
thanks for helping me but i like te Nick solution more clean and neat.
Abu Hamzah
+2  A: 

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(); } 
  });
}); 
Nick Craver
Thanks Nick....
Abu Hamzah