13

Is there a way to know the date of a transaction by looking at the raw blockchain data?

I know that Etherscan and other blockchain explorers have dates for the transactions, but I couldn't find a way to do it via web3.

web3.eth.getTransaction, and other transaction related APIs don't seem to be exposing any Date objects.

13

web3.eth.getTransaction({txhash}) will contain a blockNumber.

Use web3.eth.getBlock to retrieve the block details and you will find the following field:

 timestamp: Number - the unix timestamp for when the block was collated.

This is a Unix timestamp.

Example using geth for the following transaction 0x5da2844afb6826d4baed6ad7e8b536c00cbc921ac147773ad056f29f2e7c1762.

> let tx = "0x5da2844afb6826d4baed6ad7e8b536c00cbc921ac147773ad056f29f2e7c1762"
> web3.eth.getTransaction(tx).blockNumber
1920050
> web3.eth.getBlock(1920050).timestamp
1469021581

And using www.unixtimestamp.com, this works out to be 07/20/2016 @ 1:33pm (UTC) which matches the etherscan.io details.

  • 4
    also you can convert it directly with javascript var dateTimeStamp = web3.eth.getBlock(1920050).timestamp //outputs 1469021581 var d = new Date(dateTimeStamp * 1000) //x1000 to convert from seconds to milliseconds var s = d.toUTCString() s = s.substring(0,s.indexOf("GMT")) + "UTC" //change the confusing 'GMT' to 'UTC' s outputs "Wed, 20 Jul 2016 13:33:01 UTC" – b25 Jul 21 '16 at 16:41
  • Just to add, to switch between timestamps and human-readable timestamps in epochconverter.com/#code there are a lot of routines for differebt languages – Hari GTT Psicolabis Aug 13 '18 at 19:09
  • This doesn't work with web3 v1.0 – saitam Sep 15 '18 at 19:35
5

To get the exact date and time, include the following code:

> var date = web3.eth.getBlock(1920050).timestamp 
1469021581
> var date1 = new Date(date*1000);
 undefined
>console.log(date1.toUTCString())
 Tue, 06 Dec 2016 09:32:13 UTC
0

This is the timestamp of a block (when it was collated) not specifically of a transaction. I guess we are still awaiting the answer.

0

While most of the answers are providing information on how to obtain the block time (and they are correct), note that the transaction only takes place once it is added in a block and that block is successfully mined. Prior to that it would still be pending i.e. it still hasn't taken place.

In fact, if you look at transactions within a block on Etherscan, they all have the same timestamp.

0

Hello I tried this and its working :

web3.eth.getBlock(BLOCKNUMBER, (err, block) => { console.log(err,block); this.setState({block}); }); this.setState({timestamp: this.state.block.timestamp});

-1

Actually none of these answers are correct.

Valid and working answer is:

web3.eth.getBlock(BLOCK_NUMBER_HERE, (error, block) => {
    const timestamp = block.timestamp;

    // here you go
});
  • Downvoter care to explain? :D – Daniel Kmak Nov 8 '18 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.