简体   繁体   中英

highcharts: convert useUTC=true milliseconds to javascript Date object

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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM