Skip to content

Commit d2732ae

Browse files
Clean up date range controls
1 parent 30f45c6 commit d2732ae

File tree

2 files changed

+45
-25
lines changed

2 files changed

+45
-25
lines changed

index.html

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -795,19 +795,38 @@ <h3 id="rest_examples">Time Restraints</h3>
795795
maxDate: new Date(2010, 11, 31, 17, 30)
796796
});
797797
</pre>
798-
</div>
798+
</div>
799799

800800
<!-- ============= example -->
801801
<div class="example-container">
802-
<p>Restrict a start and end date (also shows using onSelect and onClose events):</p>
802+
<p>Restrict a start and end date with timepicker's built in restraint methods:</p>
803803
<div>
804804
<input type="text" name="rest_example_4_start" id="rest_example_4_start" value="" />
805805
<input type="text" name="rest_example_4_end" id="rest_example_4_end" value="" />
806-
</div>
806+
</div>
807807
<pre>
808808
var startDateTextBox = $('#rest_example_4_start');
809809
var endDateTextBox = $('#rest_example_4_end');
810810

811+
$.timepicker.datetimeRange(startDateTextBox, endDateTextBox, {
812+
reformat: true,
813+
start: { }, // start datetime options
814+
end: { } // end datetime options
815+
});
816+
</pre>
817+
</div>
818+
819+
<!-- ============= example -->
820+
<div class="example-container">
821+
<p>Restrict a start and end date by using onSelect and onClose events for more control over functionality:</p>
822+
<div>
823+
<input type="text" name="rest_example_5_start" id="rest_example_5_start" value="" />
824+
<input type="text" name="rest_example_5_end" id="rest_example_5_end" value="" />
825+
</div>
826+
<pre>
827+
var startDateTextBox = $('#rest_example_5_start');
828+
var endDateTextBox = $('#rest_example_5_end');
829+
811830
startDateTextBox.datetimepicker({
812831
onClose: function(dateText, inst) {
813832
if (endDateTextBox.val() != '') {
@@ -843,7 +862,6 @@ <h3 id="rest_examples">Time Restraints</h3>
843862
</pre>
844863
</div>
845864

846-
847865
<h3 id="utility_examples">Utilities</h3>
848866

849867
<!-- ============= example -->

jquery-ui-timepicker-addon.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,8 +1920,8 @@
19201920
* @param string method Can be used to specify the type of picker to be added
19211921
* @return jQuery
19221922
*/
1923-
$.timepicker.dateTimeRange = function(startTime, endTime, options) {
1924-
$.timepicker.dateRange(startTime, endTime, options, 'datetimepicker');
1923+
$.timepicker.datetimeRange = function(startTime, endTime, options) {
1924+
$.timepicker.handleRange('datetimepicker', startTime, endTime, options);
19251925
};
19261926

19271927
/**
@@ -1931,12 +1931,10 @@
19311931
* @param Element endTime
19321932
* @param obj options Options for the `timepicker()` call. Also supports `reformat`,
19331933
* a boolean value that can be used to reformat the input values to the `dateFormat`.
1934-
* @param string method Can be used to specify the type of picker to be added
19351934
* @return jQuery
19361935
*/
1937-
$.timepicker.dateRange = function(startTime, endTime, options, method) {
1938-
method = method || 'datepicker';
1939-
$.timepicker.handleRange(method, startTime, endTime, options);
1936+
$.timepicker.dateRange = function(startTime, endTime, options) {
1937+
$.timepicker.handleRange('datepicker', startTime, endTime, options);
19401938
};
19411939

19421940
/**
@@ -1952,50 +1950,54 @@
19521950
$.timepicker.handleRange = function(method, startTime, endTime, options) {
19531951
$.fn[method].call(startTime, $.extend({
19541952
onClose: function(dateText, inst) {
1955-
checkDates(this, endTime, dateText);
1953+
checkDates($(this), endTime, dateText);
19561954
},
19571955
onSelect: function(selectedDateTime) {
1958-
selected(this, endTime, 'minDate');
1956+
selected($(this), endTime, 'minDate');
19591957
}
19601958
}, options, options.start));
19611959
$.fn[method].call(endTime, $.extend({
19621960
onClose: function(dateText, inst) {
1963-
checkDates(this, startTime, dateText);
1961+
checkDates($(this), startTime, dateText);
19641962
},
19651963
onSelect: function(selectedDateTime) {
1966-
selected(this, startTime, 'maxDate');
1964+
selected($(this), startTime, 'maxDate');
19671965
}
19681966
}, options, options.end));
19691967
// timepicker doesn't provide access to its 'timeFormat' option,
19701968
// nor could I get datepicker.formatTime() to behave with times, so I
19711969
// have disabled reformatting for timepicker
19721970
if (method != 'timepicker' && options.reformat) {
19731971
$([startTime, endTime]).each(function() {
1974-
var format = $(this)[method].call($(this), 'option', 'dateFormat'),
1975-
date = new Date($(this).val());
1976-
if ($(this).val() && date) {
1977-
$(this).val($.datepicker.formatDate(format, date));
1972+
var $t = $(this),
1973+
format = $t[method].call($t, 'option', 'dateFormat'),
1974+
date = new Date($t.val());
1975+
if ($t.val() && date) {
1976+
$t.val($.datepicker.formatDate(format, date));
19781977
}
19791978
});
19801979
}
1980+
19811981
checkDates(startTime, endTime, startTime.val());
1982+
selected(startTime, endTime, 'minDate');
1983+
selected(endTime, startTime, 'maxDate');
19821984

19831985
function checkDates(changed, other, dateText) {
1984-
if (other.val() && (new Date(startTime.val()) > new Date(endTime.val()))) {
1986+
var startdt = startTime[method]('getDate'),
1987+
enddt = endTime[method]('getDate');
1988+
1989+
if (other.val() && startdt > enddt) {
19851990
other.val(dateText);
19861991
}
19871992
}
1988-
selected(startTime, endTime, 'minDate');
1989-
selected(endTime, startTime, 'maxDate');
19901993

19911994
function selected(changed, other, option) {
1992-
if (!$(changed).val()) {
1995+
if (!changed.val()) {
19931996
return;
19941997
}
1995-
var date = $(changed)[method].call($(changed), 'getDate');
1996-
// timepicker doesn't implement 'getDate' and returns a jQuery
1998+
var date = changed[method].call(changed, 'getDate');
19971999
if (date.getTime) {
1998-
$(other)[method].call($(other), 'option', option, date);
2000+
other[method].call(other, 'option', option, date);
19992001
}
20002002
}
20012003
return $([startTime.get(0), endTime.get(0)]);

0 commit comments

Comments
 (0)