If you're using Hardhat and doing tests, you can get the timestamp of a block where a specific transaction was included
it('should succeed - verify the timestamp when a token was minted', function () {
const accounts = await ethers.getSigners()
//mint something ~ the contract stores the block.timestamp during the mint
expect(yourTx = await yourContract.connect(accounts[1]).mintNFT(
randomParameters,
moreRandomParameters,
{value: yourMintPrice})).to.be.ok
//Print timestamp from the specific block where a TX was included
const foo = await ethers.provider.getBlock(yourTx.blockHash!)
console.log(`timestamp of the tx ${foo.timestamp}`)
//this function returns the token details stored in the contract
const mintedToken = await yourContract.getTokenDetails(mintedTokenId)
console.log(`tokenstoredTimestamp ${mintedToken.timestamp}`)
expect(mintedToken.timestamp).to.be.equal(foo.timestamp)
});
The previous answers are correct, this is just a different approach to get a bit more control if you are running parallel tests and your functions are time sensitive