Skip to content

Commit 3cdc478

Browse files
min/maxInterval options for handleRange
1 parent d424bb8 commit 3cdc478

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ <h3 id="rest_examples">Time Restraints</h3>
830830
var endDateTextBox = $('#rest_example_4_end');
831831

832832
$.timepicker.datetimeRange(startDateTextBox, endDateTextBox, {
833-
reformat: true,
833+
minInterval: (1000*60*60),
834834
start: { }, // start datetime options
835835
end: { } // end datetime options
836836
});

jquery-ui-timepicker-addon.js

+38-21
Original file line numberDiff line numberDiff line change
@@ -2001,46 +2001,55 @@
20012001
* @return jQuery
20022002
*/
20032003
$.timepicker.handleRange = function(method, startTime, endTime, options) {
2004+
options = $.extend({}, {
2005+
minInterval: 0, // min allowed interval in milliseconds
2006+
maxInterval: 0, // max allowed interval in milliseconds
2007+
start: {}, // options for start picker
2008+
end: {} // options for end picker
2009+
}, options);
2010+
20042011
$.fn[method].call(startTime, $.extend({
20052012
onClose: function(dateText, inst) {
2006-
checkDates($(this), endTime, dateText);
2013+
checkDates($(this), endTime);
20072014
},
20082015
onSelect: function(selectedDateTime) {
20092016
selected($(this), endTime, 'minDate');
20102017
}
20112018
}, options, options.start));
20122019
$.fn[method].call(endTime, $.extend({
20132020
onClose: function(dateText, inst) {
2014-
checkDates($(this), startTime, dateText);
2021+
checkDates($(this), startTime);
20152022
},
20162023
onSelect: function(selectedDateTime) {
20172024
selected($(this), startTime, 'maxDate');
20182025
}
20192026
}, options, options.end));
2020-
// timepicker doesn't provide access to its 'timeFormat' option,
2021-
// nor could I get datepicker.formatTime() to behave with times, so I
2022-
// have disabled reformatting for timepicker
2023-
if (method != 'timepicker' && options.reformat) {
2024-
$([startTime, endTime]).each(function() {
2025-
var $t = $(this),
2026-
format = $t[method].call($t, 'option', 'dateFormat'),
2027-
date = new Date($t.val());
2028-
if ($t.val() && date) {
2029-
$t.val($.datepicker.formatDate(format, date));
2030-
}
2031-
});
2032-
}
20332027

2034-
checkDates(startTime, endTime, startTime.val());
2028+
checkDates(startTime, endTime);
20352029
selected(startTime, endTime, 'minDate');
20362030
selected(endTime, startTime, 'maxDate');
20372031

2038-
function checkDates(changed, other, dateText) {
2032+
function checkDates(changed, other) {
20392033
var startdt = startTime[method]('getDate'),
2040-
enddt = endTime[method]('getDate');
2041-
2042-
if (other.val() && startdt > enddt) {
2043-
other.val(dateText);
2034+
enddt = endTime[method]('getDate'),
2035+
changeddt = changed[method]('getDate');
2036+
2037+
if(startdt !== null){
2038+
var minDate = new Date(startdt.getTime()),
2039+
maxDate = new Date(startdt.getTime());
2040+
2041+
minDate.setMilliseconds(minDate.getMilliseconds() + options.minInterval);
2042+
maxDate.setMilliseconds(maxDate.getMilliseconds() + options.maxInterval);
2043+
2044+
if(options.minInterval > 0 && minDate > enddt){ // minInterval check
2045+
endTime[method]('setDate',minDate);
2046+
}
2047+
else if(options.maxInterval > 0 && maxDate < enddt){ // max interval check
2048+
endTime[method]('setDate',maxDate);
2049+
}
2050+
else if (startdt > enddt) {
2051+
other[method]('setDate',changeddt);
2052+
}
20442053
}
20452054
}
20462055

@@ -2049,6 +2058,14 @@
20492058
return;
20502059
}
20512060
var date = changed[method].call(changed, 'getDate');
2061+
if(date !== null && options.minInterval > 0){
2062+
if(option == 'minDate'){
2063+
date.setMilliseconds(date.getMilliseconds() + options.minInterval);
2064+
}
2065+
if(option == 'maxDate'){
2066+
date.setMilliseconds(date.getMilliseconds() - options.minInterval);
2067+
}
2068+
}
20522069
if (date.getTime) {
20532070
other[method].call(other, 'option', option, date);
20542071
}

0 commit comments

Comments
 (0)