Skip to content

Instantly share code, notes, and snippets.

@samlaf
Last active October 28, 2023 20:22
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)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment