2

I am having a bit of trouble with jQuery datepicker. I need multiple ranges for date selection. For example user should be able to select a range between(d-m-y) 1/8/2015 and 5/8/2015. But also a range between: 16/8/2015 to 20/8/2015 (in the same calendar). The dates that do not match those ranges should be inactive. I ended up trying following code but with no success:

    $(function() {    
    var ds = array('1-8-2015', '16-8-2015'),
        de = array('5-8-2015', '20-8-2015');

    $('#datepicker').datepicker({
        beforeShow: function() {
            return {
                dateFormat: 'd-m-yy',
                minDate: ds,                
                maxDate: de,

            }
        }
    });
});

JSFiddle example: JSfiddle

Any ideas? Regards, Simon

2
  • Check out this:stackoverflow.com/questions/7709320/… Commented Aug 29, 2015 at 17:35
  • Thanks for your ultra fast response. However my question is a bit different :) Commented Aug 29, 2015 at 17:37

1 Answer 1

2

Well yes.. You can do it.. You can make use of beforeShowDay option of datepicker as below:

var d1s = new Date(2010, 8, 1),
    d1e = new Date(2010, 8, 5),
    d2s = new Date(2010, 8, 16),
    d2e = new Date(2010, 8, 20);
$(function() {
  $("#datepicker").datepicker({
    numberOfMonths: 1,
    beforeShowDay: function(date) {
      return [(date >= d1s && date <= d1e || date >= d2s && date <= d2e), ''];
    },
    minDate: d1s,
    maxDate: d2e
  });
});​

Source

Sign up to request clarification or add additional context in comments.

6 Comments

Ain't it possible without having to define the variable number? What I forgot to mention was that the dates is dynamically generated via PHP so I don't know how many dates there is. That's why a array would be preferable if possible :)
Let me try my best to meet your requirement.. :)
Example: daystart: ['1-8-2015', '15-8-2015'], dayend ['5-8-2015', '20-8-2015'] :)
Am sorry to say buddy.. But there isn't any such option in jquery datepicker to loop array in beforeShowDay which itself will form a loop.. There might be some plugins available in google for this particular requirement and I tried my best to achieve in all possible ways but did not find any way to pass array incrementally..
So there is no way to set multiple ranges? That sounds weird :/
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.