Ram Chowdary
Ram Chowdary

Reputation: 53

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

Upvotes: 0

Views: 2244

Answers (2)

Jurij Jazdanov
Jurij Jazdanov

Reputation: 1258

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 n a little and use it as a positive number ofc.

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

Datepicker Documentation

Upvotes: 3

&#214;zg&#252;r Kaplan
&#214;zg&#252;r Kaplan

Reputation: 2136

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

Upvotes: 2

Related Questions