6

Here I'm getting an array of date/time epochs

ApiFactory.getTweetQuotes(the_ticker).then(function(data) {
    // Create epoch array:
    for (var i = 0; i < data.data.quotes.length; i++) {                    
        vs.tweet_epochs.push(data.data.quotes[i].start_epoch);
    }

    console.log(vs.tweet_epochs);

    initChart();
    vs.loadingChart = true;
});

vs.tweet_epochs looks like this consoled out: enter image description here

I'm using Chartist and it's not printing out very pretty at the moment: enter image description here

I found this answer here Convert UTC Epoch to local date with javascript however it just added 3 more 0's to each epoch instead of convert them into dates.

If you need the Charist code:

var initChart = function() {
    var data = {

        labels: vs.tweet_epochs,
        series: [
            vs.tweet_vol
        ]
    };

    // Chart options:
    var options = {
        showPoint: true,
        showArea: true,
        lineSmooth: true,
        fullWidth: true,
        axisX: {
            showGrid: false,
            showLabel: true
        },
        axisY: {
            offset: 40,
            labelInterpolationFnc: function(value) {
                return '$' + value;
            }
        }
    };

    var chart = new Chartist.Line('.ct-chart', data, options);

}
Community
  • 1
  • 1
Leon Gaban
  • 36,509
  • 115
  • 332
  • 529

2 Answers2

10
var date = new Date(TIME)

where TIME is the epoch time in MILIseconds, i.e, you should multiply by 1000 if you have a epoch time in seconds.

I don't know how Chartist handle Date objects, but you have a lot of methods to get all readeable parts of a date:

date.getDate date.getDay date.getFullYear date.getHours date.getMilliseconds date.getMinutes date.getMonth date.getSeconds date.getTime date.getTimezoneOffset date.getUTCDate date.getUTCDay date.getUTCFullYear date.getUTCHours date.getUTCMilliseconds date.getUTCMinutes date.getUTCMonth date.getUTCSeconds date.getYear date.setDate date.setFullYear date.setHours date.setMilliseconds date.setMinutes date.setMonth date.setSeconds date.setTime date.setUTCDate date.setUTCFullYear date.setUTCHours date.setUTCMilliseconds date.setUTCMinutes date.setUTCMonth date.setUTCSeconds date.setYear date.toDateString date.toGMTString date.toISOString date.toJSON date.toLocaleDateString date.toLocaleTimeString date.toTimeString date.toUTCString

felipeaf
  • 377
  • 2
  • 11
1

Consider using momentjs to create a date using the Unix timestamp. Then you can output it however you like.

http://momentjs.com/docs/#/parsing/unix-timestamp/

HeadCode
  • 2,770
  • 1
  • 14
  • 26