Pregunta

así que tengo una marca de tiempo en MySQL que es:

SELECT `tsdate`, UNIX_TIMESTAMP( `tsdate`) FROM t1
2010-11-07 21:50:05, 1289191805

Si lo intento y utilizo el siguiente, muestra el momento equivocado / fecha

var newDate = new Date();
newDate.setTime(1289191805000);
dateString = newDate.toUTCString();
alert(dateString);

Mon, 08 Nov 2010 04:50:05 GMT

¿Cómo puedo obtener JavaScript para mostrar la fecha correcta?

Actualmente estoy usando las herramientas a Highcharts grapth, y es que muestra el mal de fecha / hora, así (que está escrito en JavaScript). No quiero cambiar el código de Highcharts, pero si me es necesario.

Gracias, Josh

¿Fue útil?

Solución 3

I found the solution at http://highslide.com/forum/viewtopic.php?f=9&t=8613

Highcharts.setOptions({
   global: {
      useUTC: false
   }
});

I also wanted to thank Anthony Grist and Seldaek for some helpful code!

var newDate = new Date();
newDate.setTime(1289191805000 - (newDate.getTimezoneOffset() * 60 * 1000));
dateString = newDate.toUTCString();
alert(dateString);

Otros consejos

It looks like you have a timezone problem. As you see the javascript one is GMT, while I suspect yours is some western US time?

Try the following in your MySQL query:

SELECT UNIX_TIMESTAMP( CONVERT_TZ( tsdate, '-07:00', 'GMT') ) FROM t1

-07:00 could be replaced by whatever timezone identifier you're in.

An alternative solution could be to do newDate.setTime(mysqlTimestamp + 7*3600000) in JavaScript to only adjust it there.

As previously mentioned, using toString will return it in local time, though it will also have additional Timezone information. A hack to display it in local time, without the additional Timezone information, is to use getTimezoneOffset() (returns a value in minutes) multiplied by sixty (to get it in seconds) multiplied by 1000 (to get it in milliseconds)

var newDate = new Date();
newDate.setTime(1289191805000 - (newDate.getTimezoneOffset() * 60 * 1000));
dateString = newDate.toUTCString();
alert(dateString);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top