如何在jQuery datetimepicker中获取utc时间

时间:2012-08-15 03:06:52

标签: jquery datetimepicker

我正在尝试使用此jQuery datetimepicker来获取日期和时间数据。 我能够得到大部分的东西(格式,显示等)。但是,我无法以UTC格式获取日期和时间。我只能以本地格式获取日期和时间。

有谁知道如何修改它以获取日期和时间? 或者从弹出窗口中删除“现在”按钮?

2 个答案:

答案 0 :(得分:4)

我今天遇到了同样的问题,并找到了这个旧线程。您可以在单击“立即”按钮时覆盖$ .datepicker._gotoToday以使用UTC时间。

//make the now button go to "now" in utc, not local
$.datepicker._gotoToday = function(id) {
  var inst = this._getInst($(id)[0]),
      $dp = inst.dpDiv;
  this._base_gotoToday(id);
  var tp_inst = this._get(inst, 'timepicker');
  //removed -> selectLocalTimeZone(tp_inst);
  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();
};

答案 1 :(得分:1)

如果没有任何额外的库,您可以使用以下实用程序方法执行以下操作:

var dateFromPicker = getDateFromPicker();
var dateUtc = localAsUtc(dateFromPicker);
var iso = dateUtc.toISOString(); // returns "2016-12-06T00:00:00.000Z"
/**
 * <b>WARNING</b>: This method should only be used in conjunction with components that handle local dates (i.e. date pickers).
 *
 * Returns a local date constructed from a UTC date (shifts the time by the local time zone). E.g.:
 * <ul><li>2016-01-01T00:00Z (UTC) -> 2016-01-01T00:00-0100 (CVT)
 *     <li>2016-01-01T00:00Z (UTC) -> 2016-01-01T00:00+0100 (CET)
 * </ul>
 * @param date a date in UTC time zone
 * @returns {Date} the same date & time in the local time zone
 */
function utcAsLocal(date) {
    if (isNotValidDate(date)) {
        return null;
    }

    return new Date(
        date.getUTCFullYear(),
        date.getUTCMonth(),
        date.getUTCDate(),
        date.getUTCHours(),
        date.getUTCMinutes(),
        date.getUTCSeconds(),
        date.getUTCMilliseconds()
    );
}

/**
 * <b>WARNING</b>: This method should only be used in conjunction with components that handle local dates (i.e. date pickers).
 *
 * Returns a UTC date constructed from a local date (shifts the time by the local time zone). E.g.:
 * <ul><li>2016-01-01T00:00-0100 (CVT) -> 2016-01-01T00:00Z (UTC)
 *     <li>2016-01-01T00:00+0100 (GMT) -> 2016-01-01T00:00Z (UTC)
 * </ul>
 * @param date a date in UTC time zone
 * @returns {Date} the same date & time in the UTC time zone
 */
function localAsUtc(date) {
    if (isNotValidDate(date)) {
        return null;
    }

    return new Date(Date.UTC(
        date.getFullYear(),
        date.getMonth(),
        date.getDate(),
        date.getHours(),
        date.getMinutes(),
        date.getSeconds(),
        date.getMilliseconds()
    ));
}

function isValidDate (date) {
    return !isNotValidDate(date);
}

function isNotValidDate(date) {
    return date == null || isNaN(date.getTime());
}

以下是一些模拟UTC之前和之后的时区的示例:

&#13;
&#13;
var date;

// simulate a datepicker in (CET)
date = new Date("2016-12-06T00:00:00.000+0100");
date.toISOString(); // "2016-12-05T23:00:00.000Z"
date = localAsUtc(date);
date.toISOString(); // "2016-12-06T00:00:00.000Z" sent to server

// simulate a datepicker in (CVT)
date = new Date("2016-12-06T00:00:00.000-0100");
date.toISOString(); // "2016-12-06T01:00:00.000Z"
date = localAsUtc(date);
date.toISOString(); // "2016-12-06T00:00:00.000Z" sent to server

// setting the datepicker date (CET)
date = new Date("2016-12-06T00:00:00.000Z"); // received from server
date.toISOString(); // "2016-12-06T00:00:00.000Z"
date = utcAsLocal(date); // set datepicker with this date shows (06/12/2016)
date.toISOString(); // "2016-12-05T23:00:00.000Z"

// setting the datepicker date (CVT)
date = new Date("2016-12-06T00:00:00.000Z"); // received from server
date.toISOString(); // "2016-12-06T00:00:00.000Z"
date = utcAsLocal(date); // set datepicker with this date shows (06/12/2016)
date.toISOString(); // "2016-12-06T01:00:00.000Z"
&#13;
&#13;
&#13;

但如果不提及moment.js,那么答案就不会完整,这使得处理日期变得更加轻松。