1

I'm using highcharts with datetime x-axis with useUTC=true. So to get Nov 05 2013 11:30 I use the value 1383651000000 as I found in this question highcharts datetime axis, how to comput correct timestamp?

Now when I make a selection on highcharts the event I get has timestamp in UTC. I want to convert it to a javascript Date which should be 11:30 in localtime... How do I get it?

Here is a Fiddle http://jsfiddle.net/2BffA/7/ where you see that I cannot simply construct a

new Date(event.min) 

from timestamp because I get the UTC date converted to local time.

Community
  • 1
  • 1
Emanuele Paolini
  • 9,912
  • 3
  • 38
  • 64

2 Answers2

1

I would parse the date out using something like this:

http://jsfiddle.net/2BffA/15/

making use of

date.getUTCHours()

You can change the functions to display the date whichever way you like, of course.

EDIT::

see here for an updated approach that returns a date object displaying the UTC time (though it messes with the date object, since the object still thinks the time it is showing is the local time instead of UTC):

http://jsfiddle.net/2BffA/17/

I would maintain that it is better to work with the object as is, however, and only do the conversion when you are displaying the string. The object already knows the UTC time, but displays the local time.

jlbriggs
  • 17,612
  • 4
  • 35
  • 56
  • What I really need is to get a javascript Date object to work with. Not a string. Also you cannot change only the hours field to get the correct date. I must take into account the possibility that the day is different and the possibility that the local timezone differs by half-hours with UTC. – Emanuele Paolini Nov 10 '13 at 16:18
  • the Date object that you get includes the information about the local time zone offset... Altering the object to instead display the minus the offset could lead to some screwy things if you're not careful. However, see updated answer for another approach. – jlbriggs Nov 10 '13 at 23:49
  • You are right. At the end I decided to keep my timestamp information using a float variable instead of a Date one. The point is that I need to handle "naife" datetime objects (python terminology): i.e. timestamps that should be displayed the same independently of the timezone of the client. – Emanuele Paolini Nov 12 '13 at 06:28
-1

You can also return value, which will be parsed by dateFormat.

http://api.highcharts.com/highcharts#Highcharts.dateFormat()

Sebastian Bochan
  • 37,348
  • 3
  • 49
  • 75