c3js 如何将时间戳从纪元以来的秒数格式化为正常日期?
c3js How does one format a timestamp from seconds since the epoch to a normal date?
这是我的 fiddle 和相关代码:http://jsfiddle.net/hfznh45w/13/
所以,我正在使用 C3.js,我正在尝试制作时间序列图,但我遇到了日期格式问题。
这是我用来将纪元以来的秒数转换为日期字符串的方法:
function dateToString(e) {
var date = new Date(e * 1000);
return date.toDateString().substring(4);
}
下面是我调用上述函数的方式:
x1: {
type: 'timeseries',
show: true,
tick: {
fit: true,
format: function(e, d){
return dateToString(e)
}
}
},
但似乎这些函数从未被调用(console.logs 当我将它们放入函数中时从未被调用)。
我的建议是在数据级别将数据转换为正确的日期格式
regTimes = [];
incTimes = [];
registrationTimes.forEach(function(e){regTimes.push(new Date(e * 1000))});
incomeTimes.forEach(function(e){incTimes.push(new Date(e * 1000))});
现在将 regTimes 和 incTimes 作为如下数据提供:
columns: [
['x1'].concat(regTimes),
['x2'].concat(incTimes),
['Registrations'].concat(registrations),
['Income'].concat(incomes)
],
同时提供您选择的日期刻度格式,如下所示:
tick: {
format: function (x) { return x.getSeconds(); }
}
完整的工作代码here
希望对您有所帮助!
这是我的 fiddle 和相关代码:http://jsfiddle.net/hfznh45w/13/
所以,我正在使用 C3.js,我正在尝试制作时间序列图,但我遇到了日期格式问题。
这是我用来将纪元以来的秒数转换为日期字符串的方法:
function dateToString(e) {
var date = new Date(e * 1000);
return date.toDateString().substring(4);
}
下面是我调用上述函数的方式:
x1: {
type: 'timeseries',
show: true,
tick: {
fit: true,
format: function(e, d){
return dateToString(e)
}
}
},
但似乎这些函数从未被调用(console.logs 当我将它们放入函数中时从未被调用)。
我的建议是在数据级别将数据转换为正确的日期格式
regTimes = [];
incTimes = [];
registrationTimes.forEach(function(e){regTimes.push(new Date(e * 1000))});
incomeTimes.forEach(function(e){incTimes.push(new Date(e * 1000))});
现在将 regTimes 和 incTimes 作为如下数据提供:
columns: [
['x1'].concat(regTimes),
['x2'].concat(incTimes),
['Registrations'].concat(registrations),
['Income'].concat(incomes)
],
同时提供您选择的日期刻度格式,如下所示:
tick: {
format: function (x) { return x.getSeconds(); }
}
完整的工作代码here
希望对您有所帮助!