36

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.

6 Answers 6

38

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
  • 8
    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"
    – user3498
    Commented Jul 21, 2016 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 Commented Aug 13, 2018 at 19:09
  • This doesn't work with web3 v1.0
    – sunwarr10r
    Commented Sep 15, 2018 at 19:35
  • web3.eth.getTransaction(tx) returns a Promise so web3.eth.getTransaction(tx).blockNumber is not correct. It should be let { blockNumber } = await web3.eth.getTransaction(tx) instead. Commented May 9, 2022 at 18:31
9

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
3

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
1

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

1
  • 1
    The time of the block and the transaction are the same, since all the transactions in the block are committed at the same moment as the block Commented Feb 20, 2021 at 20:59
0

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
});
2
  • Downvoter care to explain? :D
    – Daniel
    Commented Nov 8, 2018 at 12:29
  • Upvoted purely because the downvoter never provided an explanation
    – Jacques
    Commented Aug 21, 2022 at 11:32
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});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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