如何在 jquery UI datetimepicker 上设置“现在”按钮以设置 UTC 时间?

gen*_*eek 5 jquery datepicker datetimepicker timepicker jquery-ui-datepicker

我在这里使用日期时间选择器。如何设置现在按钮,以便它现在设置时间 UTC 而不是每个浏览器的当前现在?

Yas*_*ser 5

打开您的 jQuery timepicker 插件文件并转到以下功能

/*
* override "Today" button to also grab the time.
*/
$.datepicker._base_gotoToday = $.datepicker._gotoToday;
$.datepicker._gotoToday = function (id) {
    var inst = this._getInst($(id)[0]),
        $dp = inst.dpDiv;
    this._base_gotoToday(id);
    var tp_inst = this._get(inst, 'timepicker');
    selectLocalTimezone(tp_inst);
    var now = new Date();
    this._setTime(inst, now);
    $('.ui-datepicker-today', $dp).click();
};
Run Code Online (Sandbox Code Playgroud)

只需添加以下更改,

var now = new Date();
var utcNow = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
this._setTime(inst, utcNow);
Run Code Online (Sandbox Code Playgroud)


gen*_*eek 1

这对于设置时间很有用,但对于设置日历上的实际日期则不太有效。

$.datepicker._gotoToday = function (id) {
    var inst = this._getInst($(id)[0]),
    $dp = inst.dpDiv;
    this._base_gotoToday(id);
    var tp_inst = this._get(inst, 'timepicker');
    var now = new Date();
    var now_utc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
    this._setTime(inst, now_utc);
    $('.ui-datepicker-today', $dp).click();
};
Run Code Online (Sandbox Code Playgroud)