0

How can I set a datepicker's date to the first Saturday available?

Here's my code:

jQuery('input#data_da').datepicker({
  dateFormat: 'dd-mm-yy' ,
  minDate:  first saturday available
  defaultDate: 0,
  showOn: 'both',
  buttonImage: "/pw/images/sfondo_calendario.png",
  // ...
});
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Clorge32
  • 341
  • 1
  • 3
  • 7
  • Maybe duplicate of: http://stackoverflow.com/questions/4156434/javascript-get-the-first-day-of-the-week-from-current-date – Stefan Ticu Sep 16 '16 at 14:53

2 Answers2

1

You Question bit confusing, what you are asking?

I guess you want to disable all Saturday expect first Saturday of month. if yes. you need to use beforeShowDay option.

jQuery("#datepicker").datepicker({
   dateFormat: 'dd-mm-yy' ,
   defaultDate: 0,
   showOn: 'both',
   buttonText: "Select Date",
   beforeShowDay: function (date) {
     var day = date.getDay();
     var first_week = date.getDate();
     if(first_week <= 7 && day == 6){ // to find first saturday
        return  [true,''];
     }
     if(day == 6){
       return  [false,'']; // disable all saturday
     }
     return  [true,''];
    }
  });

DEMO

Sathish
  • 2,440
  • 1
  • 11
  • 27
  • for example today is 16/09/2016 so i want to show 17/09/2016 in datepiker's label automatically – Clorge32 Sep 16 '16 at 15:51
  • so you dont want to hide any dates? you need to show upcoming first saturday date as default in input box right? – Sathish Sep 16 '16 at 16:04
  • p.s your demo does the opposite that i want to do, also it doesn't show the date in the input label automatically – Clorge32 Sep 19 '16 at 07:27
0

I found the prefect solution :

  jQuery('input#date').datepicker('setDate',  get_first_saturday(new Date));
    function get_first_saturday(d) {
        var day = d.getDay();
        var numberOfDaysToAdd = 6 - day;
                firstsaturday= d.getDate() + numberOfDaysToAdd  ;
        return new Date(d.setDate(firstsaturday));
    }
Clorge32
  • 341
  • 1
  • 3
  • 7