For Ganache, there are severalsolutions.
What about Hardhat? They implemented their own local blockchain, Hardhat Network, which is different to Ganache.
- 1The buidlerevm page says it supports ganache's
evm_increaseTimeand additionally they haveevm_setNextBlockTimestamp.2020-08-12 00:16:07 +00:00CommentedAug 12, 2020 at 0:16 - @Ismael your comment is a legitimate answer. Feel free to post it :)Paul Razvan Berg– Paul Razvan Berg2020-08-12 09:43:01 +00:00CommentedAug 12, 2020 at 9:43
- I can't test right now, but I'll add an answer when I've more time.2020-08-12 21:12:12 +00:00CommentedAug 12, 2020 at 21:12
6 Answers6
Using Hardhat Network Helpers
The easiest way to do this is to use the time helpers inHardhat Network Helpers.
Install them with:
npm install @nomicfoundation/hardhat-network-helpersAnd then you can use them like this:
import { time } from "@nomicfoundation/hardhat-network-helpers";async function main() { // advance time by one hour and mine a new block await helpers.time.increase(3600); // mine a new block with timestamp `newTimestamp` await helpers.time.increaseTo(newTimestamp); // set the timestamp of the next block but don't mine a new block await helpers.time.setNextBlockTimestamp(newTimestamp);}main();You can check the referencehere.
Using raw JSON-RPC calls
There are two relevant RPC methods here:evm_increaseTime andevm_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.
Examples
evm_increaseTime
// suppose the current block has a timestamp of 01:00 PMawait network.provider.send("evm_increaseTime", [3600])await network.provider.send("evm_mine") // this one will have 02:00 PM as its timestampevm_setNextBlockTimestamp
await network.provider.send("evm_setNextBlockTimestamp", [1625097600])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 hasKeep in mind that Hardhat Network validates that the new timestamp is bigger than the previous one, so you can't sendany value.
- When using this with hardhat I got this issue: Error: Transaction reverted and Hardhat couldn't infer the reason. Please report this to help us improve Hardhat.Julien Kode– Julien Kode2021-03-07 17:16:27 +00:00CommentedMar 7, 2021 at 17:16
- Did anyone face the same issue ?Julien Kode– Julien Kode2021-03-07 17:16:35 +00:00CommentedMar 7, 2021 at 17:16
- @Franco this way also works: await network.provider.send("evm_mine", [timestampInSeconds]);Russo– Russo2021-10-01 09:27:32 +00:00CommentedOct 1, 2021 at 9:27
- What's the difference between using
ethers.providerrather thannetwork.provider? I used both and they both seem to work.Adrian D.– Adrian D.2022-01-31 19:51:27 +00:00CommentedJan 31, 2022 at 19:51 - For low-level JSON-RPC calls like these, they are the same.
network.provideris "just" an EIP-1193 provider object.ethers.provideralso implements that interface, but it has more functionality. I wrote about that at length here if you're interested:hackmd.io/@fvictorio/hardhat-networks-and-providersFranco Victorio– Franco Victorio2022-02-02 00:05:44 +00:00CommentedFeb 2, 2022 at 0:05
Anew update to Ganache has added a time parameter to theevm_mine command. Now the best way to move time is
await ethers.provider.send("evm_mine", [newTimestampInSeconds]);It is better because you only make 1 RPC call instead of 2 in theaccepted solution.
Note that you can't move time backwards in Hardhat.
- Thanks for your answer! But this question was about Hardhat, not Ganache.Paul Razvan Berg– Paul Razvan Berg2021-11-03 20:06:56 +00:00CommentedNov 3, 2021 at 20:06
- 2My answer was written for hardhat in mind, I linked to the ganache update since hardhat docs for
evm_minejust saysame as ganachewhich lead me to believe hardhat reuses some parts of ganache-cli. I am using the solution in my tests with hardhat, and they work perfectly :)hardhat.org/hardhat-network/reference/…Arjun Nemani– Arjun Nemani2021-11-05 08:49:58 +00:00CommentedNov 5, 2021 at 8:49 - 1github.com/trufflesuite/ganache/pull/13/files Found the ticket from the active ganache repoandy4thehuynh– andy4thehuynh2021-12-16 20:27:45 +00:00CommentedDec 16, 2021 at 20:27
const { expect } = require("chai");const { ethers } = require('hardhat');const sevenDays = 7 * 24 * 60 * 60;const blockNumBefore = await ethers.provider.getBlockNumber();const blockBefore = await ethers.provider.getBlock(blockNumBefore);const timestampBefore = blockBefore.timestamp;await ethers.provider.send('evm_increaseTime', [sevenDays]);await ethers.provider.send('evm_mine');const blockNumAfter = await ethers.provider.getBlockNumber();const blockAfter = await ethers.provider.getBlock(blockNumAfter);const timestampAfter = blockAfter.timestamp;expect(blockNumAfter).to.be.equal(blockNumBefore + 1);expect(timestampAfter).to.be.equal(timestampBefore + sevenDays);For any future wanderers:
Hardhat added a network-helpers library with convenient JS interface:
- https://hardhat.org/hardhat-network-helpers/docs/overview
- https://www.npmjs.com/package/@nomicfoundation/hardhat-network-helpers
Among other things, there are functions to change network time:https://hardhat.org/hardhat-network-helpers/docs/reference#time-helpers.
Some examples copied from the reference:
// advance time by one hour and mine a new blockawait helpers.time.increase(3600);// advance time to specific timestamp and mine a new blockawait helpers.time.increaseTo(newTimestamp);// set the timestamp of the next block but don't mine a new blockawait helpers.time.setNextBlockTimestamp(newTimestamp);What I've been using with typescript and hardhat:
import { ethers, waffle } from 'hardhat';const time = now + 86400await ethers.provider.send('evm_setNextBlockTimestamp', [now]); await ethers.provider.send('evm_mine');- This doesn't seem to work for me in hardhat currently, if I log out the result of the first call, I get the new time I'm trying to set to, but further assertions down the line don't seem to respect that new time. Also, I had to add an empty array after the
evm_mineargument since its required.Logging out thee result of theevm_minecall is 0x0snkashis– snkashis2021-05-26 19:02:06 +00:00CommentedMay 26, 2021 at 19:02 - 1Is there a simple way to get
nowinstead of(await ethers.provider.getBlock(await ethers.provider.getBlockNumber())).timestamp?Martian– Martian2021-12-18 09:22:37 +00:00CommentedDec 18, 2021 at 9:22 - @Martian just use "latest" instead of fetching the last block number. But other than that change, this is about as short as it gets.Chris Cashwell– Chris Cashwell2022-01-24 20:54:03 +00:00CommentedJan 24, 2022 at 20:54
Adding to Franco's answer, there is a plugin that abstracts some of the complexities of handling time directly.
You can look its documentation inhttps://www.npmjs.com/package/@atixlabs/hardhat-time-n-mine
- Returns
404. Is it moved to some different place? @GonzaloPetragliametadata– metadata2021-12-07 08:29:51 +00:00CommentedDec 7, 2021 at 8:29 - Fixed the link while thisPR is openGonzalo Petraglia– Gonzalo Petraglia2022-01-03 19:05:39 +00:00CommentedJan 3, 2022 at 19:05
- This is why answers w/ no content and just links are discouragedMadbreaks– Madbreaks2022-09-09 02:17:51 +00:00CommentedSep 9, 2022 at 2:17
Explore related questions
See similar questions with these tags.


