最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Convert javascript ticks to date in Flot jQuery-plugin issue - Stack Overflow

programmeradmin0浏览0评论

I have a dynamic Flot graph with dates on the x-axis and numbers on the y-axis. To get the Flot-plugin to read the date object correctly, I had to convert the dates to ticks (with getTime()). My problem is that I can't revers the ticks back to a normal date in my tooltip hover on the graph.

I've tried to revers it with this:

dateTimeObject = new Date((jsTicks - 621355968000000000) / 10000);

All I get, no matter what jsTicks is, is "Jan 02 0001 hh:mm:ss (almost current time)"

What am I doing wrong?

I have a dynamic Flot graph with dates on the x-axis and numbers on the y-axis. To get the Flot-plugin to read the date object correctly, I had to convert the dates to ticks (with getTime()). My problem is that I can't revers the ticks back to a normal date in my tooltip hover on the graph.

I've tried to revers it with this:

dateTimeObject = new Date((jsTicks - 621355968000000000) / 10000);

All I get, no matter what jsTicks is, is "Jan 02 0001 hh:mm:ss (almost current time)"

What am I doing wrong?

Share Improve this question edited Jun 18, 2014 at 15:52 Infinite Recursion 6,55528 gold badges41 silver badges51 bronze badges asked Sep 14, 2012 at 13:06 Kasper SkovKasper Skov 2,01410 gold badges33 silver badges54 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

This somewhat depends on whether you accounted for the browser's timezone or not when you were creating the data. A simplistic way to deal with this in a plotclick or plothover event is like so:

$("#placeholder").bind("plotclick", function(event, pos, item) {
    var x = item.datapoint[0],
    y = item.datapoint[1].toFixed(2);
    var dt = new Date(x);
    var label = 'At '+dt.toLocaleTimeString()+' ';
    //now display this label
}

If you are accounting for timezone in your data, you'd need to have one that looks more like this:

$("#placeholder").bind("plotclick", function(event, pos, item) {
    var x = item.datapoint[0],
    y = item.datapoint[1].toFixed(2);
    var userTZ = new Date();
    userTZ = userTZ.getTimezoneOffset()*60*1000;
    var dt = new Date(x+userTZ);
    var label = 'At '+dt.toLocaleTimeString()+' ';
    //now display this label
}

An example with timezones in place: http://jsfiddle/ryleyb/utNaJ/

发布评论

评论列表(0)

  1. 暂无评论