2012-08-15 17 views
5

estoy usando el de intentar este jQuery datetimepicker Para obtener los datos de fecha y hora. Puedo sacar la mayoría de las cosas (formato, pantalla, etc.). Sin embargo, no pude obtener la fecha y la hora en formato UTC. Solo puedo obtener la fecha y la hora en formato local.Cómo obtener la hora UTC en jQuery datetimepicker

¿Alguien sabe cómo modificarlo para obtener la fecha y la hora? o eliminar el botón "Ahora" en la ventana emergente?

Respuesta

1

Puede probar esto:

demostración en vivo:http://jsfiddle.net/Dgnjn/5/

$('#timePicker').datetimepicker({ 
    dateFormat: 'dd-mm-yy', 
    timeFormat: 'hh:mm:ss', 
    stepHour: 2, 
    stepMinute: 10, 
    onSelect: function(dateText, inst) { 
    var dateAsString = dateText; 
    var dateAsObject = $(this).datepicker('getDate'); 
    dateAsObject = $.datepicker.formatDate('mm-dd-yy', new Date(dateAsObject)) 
     alert(dateAsObject); 
    } 
}); 
+1

Gracias por la respuesta, pero el código solo nos dice cómo formatear la salida. No indica cómo cambiar el formato de hora de la hora local a la hora UTC. – user1599647

4

me encontré con el mismo problema hoy en día, y encontramos este hilo de edad. Puede anular $ .datepicker._gotoToday para usar la hora UTC al hacer clic en el botón "ahora".

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

Esto solo establece el tiempo a UTC, la fecha permanece local. Por lo tanto, si su configuración regional es el 1 ° del mes y UTC está en el 2 °, entonces el botón 'ahora' todavía usará el 1 ° como fecha. – Simon

0

Sin ningún tipo de bibliotecas adicionales se pueden utilizar los métodos de utilidad más adelante para hacer lo siguiente:

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

Éstos son algunos ejemplos que simulan las zonas de tiempo antes y después de la UTC:

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"

pero la respuesta no sería completa sin mencionar el moment.js lo que hace que el manejo fechas una diablos de mucho más fácil.