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.

BokkyPooBah
  • 40,274
  • 14
  • 123
  • 193
user3498
  • 735
  • 2
  • 9
  • 12

6 Answers6

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.

Utgarda
  • 791
  • 5
  • 20
BokkyPooBah
  • 40,274
  • 14
  • 123
  • 193
  • 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 Jul 21 '16 at 16:41
  • Just to add, to switch between timestamps and human-readable timestamps in https://www.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 – sunwarr10r Sep 15 '18 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. – Sprout Coder May 09 '22 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
Rahul Sharma
  • 1,303
  • 2
  • 16
  • 24
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.

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
    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 – Shebla Tsama Feb 20 '21 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
});
Daniel
  • 359
  • 4
  • 16
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});

Julissa DC
  • 1,888
  • 1
  • 8
  • 21
OZONE
  • 11