Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added minSpan option #2365

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions daterangepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
this.endDate = moment().endOf('day');
this.minDate = false;
this.maxDate = false;
this.minSpan = false;
this.maxSpan = false;
this.autoApply = false;
this.singleDatePicker = false;
Expand Down Expand Up @@ -206,6 +207,9 @@
if (typeof options.cancelClass === 'string') //backwards compat
this.cancelButtonClasses = options.cancelClass;

if (typeof options.minSpan === 'object')
this.minSpan = options.minSpan;

if (typeof options.maxSpan === 'object')
this.maxSpan = options.maxSpan;

Expand Down Expand Up @@ -332,6 +336,16 @@
var maxDate = this.maxDate;
if (this.maxSpan && maxDate && start.clone().add(this.maxSpan).isAfter(maxDate))
maxDate = start.clone().add(this.maxSpan);

// If minSpan exceeds maxSpan, use maxSpan instead.
if (this.minSpan && this.maxSpan && this.minSpan > this.maxSpan)
this.minSpan = this.maxSpan;

// If end date does not equal or exceed start date + minSpan,
// use the start date + minSpan as end date.
if (this.minSpan && end.clone().isBefore(start.clone().add(this.minSpan).subtract(1, 'day').endOf('day')))
end = start.clone().add(this.minSpan).subtract(1, 'day').endOf('day');

if (maxDate && end.isAfter(maxDate))
end = maxDate.clone();

Expand Down Expand Up @@ -497,6 +511,9 @@
if (this.endDate.isBefore(this.startDate))
this.endDate = this.startDate.clone();

if (this.minSpan && this.endDate.clone().isBefore(this.startDate.clone().add(this.minSpan)))
this.endDate = this.startDate.clone().add(this.minSpan).subtract(1, 'day').endOf('day');

if (this.maxDate && this.endDate.isAfter(this.maxDate))
this.endDate = this.maxDate.clone();

Expand Down