如何在 dygraph 的值格式化程序函数中将日期从纪元更改为本地

how to change date to local from epoch in value formatter function of dygraph

我正在为我的一个项目使用 dygraph,我正在使用 valueFormatter 函数将我的数据从毫秒更改为秒,问题是我的日期(在这张图片中为 1448821800))也正在更改为纪元。我想以 yyyy/mm/dd 格式显示我的日期。我怎样才能做到这一点?

这是图表的截图

和 valueFormatter 函数的 javascript 代码是:

valueFormatter: function(num){return (num/1000);}

如果您可以相信其他部分的值低于某个阈值,您可以使用以下方法:

valueFormatter: function(num){
    if (num > 100000000) {
        return moment(num).format()
    }

    return (num / 1000);
}

虽然查看了 this example 的来源,但我认为仅通过调整特定轴的 valueFormatter 可能会更具体且不那么骇人听闻。不过,为了帮助您解决这个问题,您需要查看更多的 dygraph 初始化代码。

答案使用Moment.js. An example of the above function can be found here