For Ganache, there are several solutions.
What about Hardhat? They implemented their own local blockchain, Hardhat Network, which is different to Ganache.
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.Sign up to join this community
There are two relevant RPC methods here:
evm_setNextBlockTimestamp. In both cases, they affect the next block but don't mine one.
evm_increaseTime receives a number of seconds that will be added to the timestamp of the latest block.
evm_setNextBlockTimestamp receives an absolute UNIX timestamp (again, in seconds), and so it's not affected by the current block.
// suppose the current block has a timestamp of 01:00 PM await network.provider.send("evm_increaseTime", ) await network.provider.send("evm_mine") // this one will have 02:00 PM as its timestamp
await network.provider.send("evm_setNextBlockTimestamp", ) await network.provider.send("evm_mine") // this one will have 2021-07-01 12:00 AM as its timestamp, no matter what the previous block has
Keep in mind that Hardhat Network validates that the new timestamp is bigger than the previous one, so you can't send any value.