在 Flot jQuery 插件问题中将 javascript 刻度转换为日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12425220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert javascript ticks to date in Flot jQuery-plugin issue
提问by Kasper Skov
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.
我有一个动态 Flot 图,x 轴上有日期,y 轴上有数字。为了让 Flot 插件正确读取日期对象,我必须将日期转换为刻度(带有getTime()
)。我的问题是我无法在我的工具提示悬停在图表上时将刻度反转回正常日期。
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)"
无论 jsTicks 是什么,我得到的只是“Jan 02 0001 hh:mm:ss(几乎是当前时间)”
What am I doing wrong?
我究竟做错了什么?
采纳答案by Ryley
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:
这在某种程度上取决于您在创建数据时是否考虑了浏览器的时区。在 a plotclick
orplothover
事件中处理此问题的简单方法如下:
$("#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.net/ryleyb/utNaJ/
时区到位的示例:http: //jsfiddle.net/ryleyb/utNaJ/