Search code examples
javascriptdatedygraphs

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


I am using dygraph for one of my project, i am making use of valueFormatter function to change my data to seconds from milliseconds, the problem is my date(in this pic 1448821800)) is also getting changed to epoch. I want to display my date in yyyy/mm/dd format. How can i achieve this?

Here is the screenshot of the graph enter image description here

and the javascript code for valueFormatter function is:

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


Solution

  • If you can rely on the values in the other parts being below a certain threshold, you could use the following:

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

    Though looking at the source of this example, I think it might be possible to be more specific and less hacky by only adjusting the valueFormatter for specific axes. To help you with this would need to see more of your dygraph initialisation code though.

    Answer makes use of Moment.js. An example of the above function can be found here.