How to obtain utc time in jQuery datetimepicker

user1599647 picture user1599647 · Aug 15, 2012 · Viewed 11.6k times · Source

I am using the trying this jQuery datetimepicker to obtain date and time data. I am able to get most of the things out (format, display, etc). However, I wasn't able to get the date and time in UTC format. I am only able to obtain the date and time in local format.

Does anyone know how to modify it to get the date and time? or remove off the "Now" button in the popup?

Answer

user1787024 picture user1787024 · Oct 30, 2012

I ran into the same issue today, and found this old thread. You can override $.datepicker._gotoToday to use UTC Time when you click the "now" button.

//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();
};