最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to convert epoch to readable dates in Angular to use in Chartist - Stack Overflow

programmeradmin4浏览0评论

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:

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

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

}

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:

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

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

}
Share Improve this question edited May 23, 2017 at 12:32 CommunityBot 11 silver badge asked Apr 6, 2015 at 16:59 Leon GabanLeon Gaban 39k122 gold badges348 silver badges550 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11
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

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

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

发布评论

评论列表(0)

  1. 暂无评论