Search code examples
javascriptjsonbitcoinblockchain

Outputting human readable times from timestamps on blockchain.info


I want to parse the timestamps from the JSON responses from blockchain.info's API.

Here is an example snippet https://blockchain.info/api/api_websocket

So if we look at

"time": 1331300839,

I try and do something like

var test = new Date(1331300839);
test.getFullYear();

And my results are circa 1970. I've tried using the Date object to parse recent Bitcoin transactions.. say https://blockchain.info/rawaddr/1Hy8LSovPiT3Z4qF7Hr2piJXZFHzpSBaEK And I'm still getting 1970.

So how do I get a human readable output from these timestamps?

Thanks.


Solution

  • IF you wanna format date and times, probably the best light-weight library that you can find out there is moment.js

    var parsed = moment.unix(1331300839)
    

    Now to format this moment instance, just use the format that you want from this list: http://momentjs.com/docs/#/displaying/ or use the very simple yet powerful moment.toString() or moment.fromNow()

    parsed.toString()
    // "Fri Mar 09 2012 14:47:19 GMT+0100"
    
    parsed.fromNow()
    // "2 years ago"