longbill/jquery-date-range-picker

MaxDays setting doesn't work properly

Opened this issue · 4 comments

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);
		},
	});

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.

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!

Thanks, so you think we should analyse what happens here

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

My observations:

  • 86400000 is the amount of milliseconds in one day
  • Math.ceil() rounds any value up to the nearest integer, so "0.4" becomes 1
  • due to the +1 the days var will always have at least a value of 2

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.

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 ?