longbill / jquery-date-range-picker

A jQuery plugin that allows user to select a date range
MIT License
1.12k stars 579 forks source link

MaxDays setting doesn't work properly #382

Open DeveloperWilco opened 6 years ago

DeveloperWilco commented 6 years ago

When setting the maxDays option you can't select the actual max value.

For example: maxDays: 30

Selecting exactly 30 days will give you the less-then error.

I checked the daterangepicker code and put a console.log( days + ' > ' + opt.maxDays ); right where the checkSelectionValid() function created the day variable. The console shows 31 > 30 So I selected 30 but it adds a day.

I removed the +1 from the variable days ( var days = Math.ceil((opt.end - opt.start) / 86400000) + 1; ) and its working.

Now i'm wondering what the purpose would be of that +1 anyway. It's working for me now, but if you can manage to fix that for the future, that would be awesome.

also, this is my configuration for the daterangepicker.


var mult_first = '#multiple-date-select';
var mult_last = '#multiple-date-select-2';

    jQuery('#multiple-date-select').dateRangePicker({

        maxDays: 30,
        autoClose: true,
        monthSelect: true,
            yearSelect: true,
        singleDate: false,
        showShortcuts: false,

        // 2 input fields
        getValue: function()
        {
            if (jQuery(mult_first).val() && jQuery(mult_last).val() )
                return jQuery(mult_first).val() + ' to ' + jQuery(mult_last).val();
            else
                return '';
        },
        setValue: function(s,s1,s2)
        {
            jQuery(mult_first).val(s1);
            jQuery(mult_last).val(s2);
        },
    });
holtkamp commented 6 years ago

Hi @DeveloperWilco, thanks for the detailed information. Do you think you can combine this in a running example, for example using https://jsfiddle.net

It appears all contributors are very busy / do not have this project as a priority. So making it as easy as possible for them to dive into this might help getting this resolved.

DeveloperWilco commented 6 years ago

Done!

Also figured out the problem only occurs when the maxDays setting is set on 29 or higher. Also described in the fiddle.

Thanks in advance!

holtkamp commented 6 years ago

Thanks, so you think we should analyse what happens here https://github.com/longbill/jquery-date-range-picker/blob/79f6e9149b0a120b3de3bbb508dc2711a0ca33ed/src/jquery.daterangepicker.js#L1723

My observations:

I currently do not have time to dive into this, but maybe someone else has. I always have a bit the feeling the "current time" is used when selecting the days, but have never studied this in detail.

wakirin commented 6 years ago

I can't reproduce this error.

// after
var days = Math.ceil((opt.end - opt.start) / 86400000) + 1; ` 

// added
console.log(days, opt.maxDays);

It's always show as expected. ("30, 30" when chooses 30 days and maxDays is 30). Where I'm wrong ?