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.
var dateTimeStamp = web3.eth.getBlock(1920050).timestamp //outputs 1469021581var d = new Date(dateTimeStamp * 1000) //x1000 to convert from seconds to millisecondsvar s = d.toUTCString()s = s.substring(0,s.indexOf("GMT")) + "UTC" //change the confusing 'GMT' to 'UTC'soutputs "Wed, 20 Jul 2016 13:33:01 UTC"
– user3498 Jul 21 '16 at 16:41web3v1.0 – sunwarr10r Sep 15 '18 at 19:35web3.eth.getTransaction(tx)returns aPromisesoweb3.eth.getTransaction(tx).blockNumberis not correct. It should belet { blockNumber } = await web3.eth.getTransaction(tx)instead. – Sprout Coder May 09 '22 at 18:31