简体   繁体   中英

How to select only week start date in jQuery datepicker

I am using jQuery datepicker.
I want to allow the user to select only week start dates.

For example, if today is Monday, the user should not select today date as it is not the week's start date, I am assuming Sunday is the week's start date, How can I do it?

My sample code snippet is :

$('.reservationDateStyle').datepicker({
  changeMonth: true,
  changeYear: true,
  showOn: 'button',
  buttonImage: '<%=contextPath%>/images/calPickerIcon.png',
  buttonImageOnly: true,
  onSelect: function() {
    this.focus();
  }
});

You can get the week day like :

var d = new Date();
var n = d.getDay();

n will be a number from 0 to 6 (0 is sunday, 1 is monday and so on). Then if you want to limit the select from last sunday:

$(function() {
    $( "#datepicker" ).datepicker({ minDate: -n });
});

Where min date will be -n days from today. If you wish to use next sunday as min date you just need to modify na little and use it as a positive number ofc.

n = 7 - n;
$(function() {
    $( "#datepicker" ).datepicker({ minDate: +n });
});

Datepicker Documentation

You can use beforeShowDay method like this.

jQuery(document).ready(function() {
    jQuery('#date').datepicker({
        beforeShowDay: function(date){
          return [date.getDay()===0];
        }
    });
});

http://jsbin.com/lehuyejiru/edit?html,js,console,output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM