trentrichardson / jQuery-Timepicker-Addon

Adds a timepicker to jQueryUI Datepicker
http://trentrichardson.com/examples/timepicker/
MIT License
2.66k stars 1.05k forks source link

Setting min/maxDate under certain conditions removes time #722

Closed Ceiu closed 10 years ago

Ceiu commented 10 years ago

I have a pair of time pickers representing start/stop times with values coming from both clients and from stored values on the server end. There is some event handling to set the min/maxDate and min/maxDateTime to ensure the two pickers don't crossover. This works fine for most cases, but there are some edge cases where setting the min/maxDate causes the time to be removed from the value, which also causes an "error parsing the date/time string."

As far as I can tell, this is due to min/maxDate calling _setDate internally in datepicker. Sometimes this works fine, other times the time is lost (even though its components are still stored). In my particular case, setting the min or maxDate before opening the time picker when the input has a server-provided value triggers this issue.

I've added a patch to work around this issue on my end. I've not formally tested this, so use it at your own risk. Also, this may not even be the best way to deal with this problem (basically, assume the worst :smile:).

This code goes in the _updateDateTime method, around line 885.

// if a slider was changed but datepicker doesn't have a value yet, set it
if (dp_inst.lastVal === "") {
    dp_inst.currentYear = dp_inst.selectedYear;
    dp_inst.currentMonth = dp_inst.selectedMonth;
    dp_inst.currentDay = dp_inst.selectedDay;
}

// BEGIN PATCH

// If a time isn't available, make one from the potential components values we've stored.
if (!timeAvailable && !this.formattedTime) {
    var timeFormat = $.datepicker._get(dp_inst, 'timeFormat');
    this.formattedTime = $.datepicker.formatTime(timeFormat, this);
}

// END PATCH

/*
* remove following lines to force every changes in date picker to change the input value
* Bug descriptions: when an input field has a default value, and click on the field to pop up the date picker.
* If the user manually empty the value in the input field, the date picker will never change selected value.
*/
//if (dp_inst.lastVal !== undefined && (dp_inst.lastVal.length > 0 && this.$input.val().length === 0)) {
//  return;
//}

If timeFormat doesn't end up with a bad value, it should just kick out the last known time, based on the components stored in the timepicker instance. If there isn't any time information available, this should just kick out the default "0:00" time in whatever format is specified.

trentrichardson commented 10 years ago

@Ceiu I know this is late, but please check the dev branch, it sounds like this exact bug was fixed.