Last active
October 1, 2024 03:26
-
-
Save samlaf/a8536c5c1484d1a8a7e16f960bbf5bc2 to your computer and use it in GitHub Desktop.
Get ethereum latest block timestamp in date format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// hardhat task | |
task('blockTimestamp', 'Prints the block timestamp', async (_, { ethers }) => { | |
const currentBlock = await ethers.provider.getBlockNumber(); | |
const blockTimestamp = (await ethers.provider.getBlock(currentBlock)).timestamp; | |
console.log(blockTimestamp); | |
const date = new Date(blockTimestamp * 1000); // Date requires ms, whereas block.timestamp is in s | |
console.log(date) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
115792089237316195423570985008687907853269984665640564039457584007913129639935