Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert epoch to readable dates in Angular to use in Chartist

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);

}
like image 264
Leon Gaban Avatar asked Apr 06 '15 16:04

Leon Gaban


1 Answers

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

like image 120
felipeaf Avatar answered Oct 29 '22 16:10

felipeaf