Question

so I have a timestamp in mysql that is:

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

If I try and use the following, it displays the wrong time/date

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

Mon, 08 Nov 2010 04:50:05 GMT

How can I get JavaScript to show the correct date?

I'm currently using the tool highcharts to grapth, and it is showing the wrong date/time as well (it's written in JavaScript). I don't want to change the code of highcharts, but I will if needed.

Thanks, Josh

Was it helpful?

Solution 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);

OTHER TIPS

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top