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

setDate() with a date+time string input does not use format options for parsing (1.3.1+) #730

Open rbeurskens opened 10 years ago

rbeurskens commented 10 years ago

(related to #585 )

Before version 1.3.1, I can't remember having trouble when try to set the value of the datetimepicker with a combined date+time string matching the formats set in options (dateFormat+separator+timeFormat). Code from before 1.3.1:

 $.datepicker._setDateDatepicker = function (target, date) {
var inst = this._getInst(target);
if (!inst) {
return;
}
var tp_date = (date instanceof Date) ? new Date(date.getTime()) : date;
this._updateDatepicker(inst);
this._base_setDateDatepicker.apply(this, arguments);
this._setTimeDatepicker(target, tp_date, true);
}; 

However, since 1.3.1 setDate() parses the input string by directly passing it to Javascript's Date() constructor, without taking the configured format into account: Code taken from 1.4.4:

    /*
    * override setDate() to allow setting time too within Date object
    */
    $.datepicker._base_setDateDatepicker = $.datepicker._setDateDatepicker;
    $.datepicker._setDateDatepicker = function (target, date) {
        var inst = this._getInst(target);
        if (!inst) {
            return;
        }

        if (typeof(date) === 'string') {
/* .......... */
            date = new Date(date);
/* .......... */
            if (!date.getTime()) {
                $.timepicker.log("Error creating Date object from string.");
            }
        }

        var tp_inst = this._get(inst, 'timepicker');
        var tp_date;
        if (date instanceof Date) {
            tp_date = new Date(date.getTime());
            tp_date.setMicroseconds(date.getMicroseconds());
        } else {
            tp_date = date;
        }

        // This is important if you are using the timezone option, javascript's Date 
        // object will only return the timezone offset for the current locale, so we 
        // adjust it accordingly.  If not using timezone option this won't matter..
        // If a timezone is different in tp, keep the timezone as is
        if (tp_inst && tp_date) {
            // look out for DST if tz wasn't specified
            if (!tp_inst.support.timezone && tp_inst._defaults.timezone === null) {
                tp_inst.timezone = tp_date.getTimezoneOffset() * -1;
            }
            date = $.timepicker.timezoneAdjust(date, tp_inst.timezone);
            tp_date = $.timepicker.timezoneAdjust(tp_date, tp_inst.timezone);
        }

        this._updateDatepicker(inst);
        this._base_setDateDatepicker.apply(this, arguments);
        this._setTimeDatepicker(target, tp_date, true);
    };

Maybe using $.datepicker.parseDateTime() instead of Date() constructor may solve this or should I call both setDate() and setTime() after manually splitting the string ?

(As setTime() works fine when passing the full date+time string, but it does not set the date part )

xael-fry commented 9 years ago

+1 Save Problem here, I think we must use the format we provide