简体   繁体   中英

jQuery UI DatePicker - only show 3 days of entire year

I'm wondering if it's possible to show only 3 days a year in the datepicker?

This is what I got so far:

JS:

 var availableDates = ["28-12-2017","29-12-2017","30-12-2017"];

 function available(date) {
   dmy = date.getDate() + "-" + (date.getMonth()+1) + "-" + 
   date.getFullYear();
   if ($.inArray(dmy, availableDates) !== -1) {
     return [true, "","Available"];
   } else {
     return [false,"","unAvailable"];
   }
 }

 $('#date').datepicker({ beforeShowDay: available }); 

css:

.ui-datepicker-unselectable {display: none;}

jsfiddle link

Dear you forgot defaultDate , please try this:

  var availableDates = ["28-12-2017","29-12-2017","30-12-2017"];

  function available(date) {
   dmy = date.getDate() + "-" + (date.getMonth()+1) + "-" + date.getFullYear();
  if ($.inArray(dmy, availableDates) !== -1) {
      return [true, "","Available"];
    } else {
      return [false,"","unAvailable"];
    }
  }

  $('#date').datepicker({ beforeShowDay: available, defaultDate: "12/28/2017" }); 

Please use this fiddle http://jsfiddle.net/szYYY/50/

I would suggest you to remove the hardcoded date and make the code more generic:

JS:

 var availableDates = ["28-12-2017", "29-12-2017", "30-12-2017"];

 function available(date) {
   dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
   if ($.inArray(dmy, availableDates) !== -1) {
     return [true, "", "Available"];
   } else {
     return [false, "", "unAvailable"];
   }
 }

 $('#date').datepicker({
   beforeShowDay: available,
   dateFormat: "dd-mm-yy",
   minDate: availableDates[0],
   maxDate: availableDates[2]
 });

No CSS required.

Displaying the full month with disabled dates gives clarity to the user rather than hiding the dates.

Demo: http://jsfiddle.net/szYYY/52/

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