根据周限制日历

时间:2016-06-22 08:18:24

标签: javascript jquery datepicker calendar

我使用日历。我希望在用户选择日期时限制日历,那么日期选择必须是每周基本我怎么做的?天可能是随机的,例如周一至周日或周二至周一等..必须为7天

     <script type="text/javascript">

      $(function() {
  var currentYear = (new Date).getFullYear();
  var currentMonth = (new Date).getMonth();
  var currentDay = (new Date).getDate();

  $("#fromdate").datepicker({
    minDate: new Date((currentYear - 1), 12, 1),
    dateFormat: 'dd/mm/yy',
    maxDate: new Date(currentYear, currentMonth, currentDay)
  });
  $("#todate").datepicker({
    minDate: new Date((currentYear - 1), 12, 1),
    dateFormat: 'dd/mm/yy',
    maxDate: new Date(currentYear, currentMonth, currentDay)
  });
});
    </script> 

HTML

 <asp:Label ID="Label1" runat="server" Text="From Date"></asp:Label>

  <input  ID="fromdate" runat="server" ClientIDMode="static" />

 <asp:Label ID="Label2" runat="server" Text="To Date"></asp:Label>

  <input  ID="todate" runat="server" ClientIDMode="static" />

1 个答案:

答案 0 :(得分:1)

试试这个,

$(function() {
      var currentYear = (new Date).getFullYear();  
      $('#fromdate').datepicker({
        minDate: 0,
        dateFormat: 'dd/mm/yy',
        maxDate: new Date((currentYear + 1), 12, 1),
        onSelect: function(selectedDate) {

            // Start Date
            var startDate = $(this).datepicker('getDate');
            $('#todate').datepicker('option', 'minDate', startDate);
            $('#todate').datepicker('setDate', startDate);

            // End Date
            var enddate = $(this).datepicker('getDate');
            enddate.setDate(enddate.getDate()+ 6);
            $('#todate').datepicker('option', 'maxDate', enddate);
      }});

      $('#todate').datepicker({
        minDate: 0,
        dateFormat: 'dd/mm/yy',
        maxDate: '+6'
    });

});