1

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

2
  • What time zone is the timestamp supposed to be in? Is it UTC too? Commented Jan 13, 2011 at 14:58
  • If you live in a place that is in the GMT-7 timezone, using .toString() will produce the correct time! Commented Jan 13, 2011 at 15:01

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

But I would prefer not to hard code the timezone. What if someone else views data from -8? or -5?
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.