Skip to content

Instantly share code, notes, and snippets.

@samlaf
Last active October 1, 2024 03:26
Show Gist options
  • Save samlaf/a8536c5c1484d1a8a7e16f960bbf5bc2 to your computer and use it in GitHub Desktop.
Save samlaf/a8536c5c1484d1a8a7e16f960bbf5bc2 to your computer and use it in GitHub Desktop.
Get ethereum latest block timestamp in date format
// 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)
});
@AlexBlessing
Copy link

115792089237316195423570985008687907853269984665640564039457584007913129639935

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment