【问题标题】:c3js How does one format a timestamp from seconds since the epoch to a normal date?c3js 如何格式化从纪元以来的秒数到正常日期的时间戳?
【发布时间】:2015-10-30 12:47:18
【问题描述】:

这是我的相关代码: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 永远不会被调用)。

【问题讨论】:

    标签: javascript d3.js c3.js


    【解决方案1】:

    我的建议是在数据级别将数据转换为正确的日期格式

    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

    希望这会有所帮助!

    【讨论】:

    • 感谢 jsfiddle。这有很大帮助!虽然我不希望秒作为我的标签,但让日期时间正确解析是主要问题。一旦格式/标签间距满意,我会根据需要发布我的 jsfiddle。
    猜你喜欢
    • 2020-11-17
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 2011-05-31
    • 2019-05-07
    相关资源
    最近更新 更多