Hyperledger Fabric Ledger History Timestamp Date Formatting in NODE JS.

Simsonraj Easvarasakthi
1 min readOct 9, 2019

--

When the Fabric’s Ledger is queried for the historical transaction data of an asset, you might notice the “timestamp” is in puzzling JSON format, when the chain code is written in Node.js Shim package, looking similar to shown below:

"Timestamp":{ “seconds”: { "low": 1570417284, "high": 0, "unsigned": false}, “nanos”: 435000000}

The format stored above is converted from int64 number because in javascript there exists no 64 bit long datatype. So the mentioned format is just expressing the timestamp value which was originally in 64bit length by dividing it into two 32bit length value.

  • High: Upper 32bit
  • Low: Lower 32bit
  • Unsigned: signed(false) / unsigned(true)

The solution is simple, on how to normalize it into readable formats for both Golang and Node SDK as shown below:

GO:

func (c *context) Time() (time.Time, error) {   txTimestamp, err := c.stub.GetTxTimestamp()   if err != nil {     return time.Unix(0, 0), err   }   return time.Unix(txTimestamp.GetSeconds(),       int64(txTimestamp.GetNanos())), nil}

Node.js

function toDate(timestamp) {  const milliseconds = (timestamp.seconds.low + ((timestamp.nanos / 1000000) / 1000)) * 1000;  return new Date(milliseconds);}

Hope this helps for that one guy who pulls up every google link in the hope of coming across this. :D

--

--