trentrichardson / jQuery-Timepicker-Addon

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

setDate with timezone works wrongly #626

Open vizv opened 11 years ago

vizv commented 11 years ago

When try to set date with timezone provided, the datetimepicker won't work.

The following example is expected to have same text and int after click 'get' and 'set' buttons many times, however is won't work for current version.

HTML Part:

<label for="foo">Text:</label>
<input type="text" name="foo" id="foo" style="width:100%">
<div style="text-align: right;">
    <button id="get">Get</button>
    <button id="set">Set</button>
</div>
<label for="bar">Int:</label>
<input type="text" name="bar" id="bar" style="width:100%">

Javascript Part:

var $fooText = $("#foo");
var $barInt = $("#bar")

$fooText.datetimepicker({timezone: "+0000"});

$("#get").click(function () {
    var date = $fooText.datetimepicker("getDate");
    console.log(date);
    $barInt.val(date.getTime());
});

$("#set").click(function () {
    var date = new Date(parseInt($barInt.val()));
    console.log(date);
    $fooText.datetimepicker("setDate", date);
});

$fooText.datetimepicker("setDate", new Date());

Check here: http://jsfiddle.net/28tGW/4/


My temporary fix (for 18de616243dae142bcef4c7bcbe050348454ca57) is done by

--- jquery-ui-timepicker-addon.js   2013-08-14 16:42:25.987734583 -0300
+++ jquery-ui-timepicker-addon-fix.js   2013-08-14 16:44:39.479922962 -0300
@@ -1605,8 +1605,8 @@
            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);
+           date = $.timepicker.timezoneAdjust(date, tp_inst.timezone, true);
+           tp_date = $.timepicker.timezoneAdjust(tp_date, tp_inst.timezone, true);
        }

        this._updateDatepicker(inst);
@@ -1645,7 +1645,7 @@
                    if (!tp_inst.support.timezone && tp_inst._defaults.timezone === null) {
                        tp_inst.timezone = date.getTimezoneOffset() * -1;
                    }
-                   date = $.timepicker.timezoneAdjust(date, tp_inst.timezone);
+                   date = $.timepicker.timezoneAdjust(date, tp_inst.timezone, false);
                }
            }
            return date;
@@ -1963,10 +1963,10 @@
     * @param  {string} toTimezone formatted like "+0500", "-1245"
     * @return {Date}
     */
-   $.timepicker.timezoneAdjust = function (date, toTimezone) {
+   $.timepicker.timezoneAdjust = function (date, toTimezone, fix) {
        var toTz = $.timepicker.timezoneOffsetNumber(toTimezone);
        if (!isNaN(toTz)) {
-           date.setMinutes(date.getMinutes() + -date.getTimezoneOffset() - toTz);
+           date.setMinutes(date.getMinutes() + (fix?1:-1) * date.getTimezoneOffset() - toTz);
        }
        return date;
    };

However, you don't want merge it directly ;)

michielvv commented 10 years ago

For me the proposed fix works as expected.