简体   繁体   中英

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.

According to the documentation for Date , dates are defined in milliseconds instead of seconds, so you'll need to multiply your timestamp by 1000.

Running the following results in an alert with the text "2012".

var test = new Date(1331300839*1000);
alert(test.getFullYear());

Like Adrian, I would also recommend Moment.js if you want to get fancy with converting timestamps to human readable strings.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM