0

How to show only specific days for all months and year in a jquery datepicker. I want to enable only 2 and 16 days for all months. no restriction for year and months. If it is possible give me solution. http://jsfiddle.net/yXMKC/4245/

Js:

var available_formatted_days_list = ["07-02-2018", "07-16-2018"];
function check_available_date( date ) {
    var formatted_date = '', ret = [true, "", ""];
    if (date instanceof Date) {
        formatted_date = $.datepicker.formatDate( 'mm-dd-yy', date );
    } else {
        formatted_date = '' + date;
    }
    if ( -1 === available_formatted_days_list.indexOf(formatted_date) ) {
        ret[0] = false;
        ret[1] = "date-disabled"; 
        ret[2] = "Date not available"; // put your custom message here
    }
    return ret;
}

$('#date').datepicker({
    dateFormat: 'dd-mm-yy',
    beforeShowDay: check_available_date
});

HTML:

<input type="text" name="date" id="date" readonly="readonly" size="12" />
Zeeshan Adil
  • 1,937
  • 5
  • 23
  • 42
Apple Orange
  • 646
  • 5
  • 12
  • 27

2 Answers2

1

You need to make some changes in order for it to work.

  1. First make you available_formatted_days_list array a list of numbers as

    available_formatted_days_list = [2, 16]

  2. Get the current date via the Date API

    let currentDay = date.getDate();

  3. Check with this value instead

    if ( -1 === available_formatted_days_list.indexOf(currentDay) )

http://jsfiddle.net/aurc7xwp/

Hope this helps!

1

Here is my solution

$('#date').datepicker({
    dateFormat: 'dd-mm-yy',
    beforeShowDay: function(d) {
    console.log("in"+d.getDate());
       if (d.getDate() == 2 || d.getDate() == 16) {
         return [true, "" ];
       } else {
          return [false, "" ];
       }
    }
});
Jax Teller
  • 1,447
  • 2
  • 15
  • 24