Now in Solidity

What is Now in Solidity

As we know Solidity is the language we use to write programs and applications more commonly known as Decentralized Applications (dApps). These apps are called decentralized because they are programmed to run on the Blockchain. The Blockchain is a decentralized entity itself and hence anything running on it has to be decentralized as well. On the Blockchain, many transactions take place all the time. Sometimes, you might want your smart contract to respond differently to transactions happening on the Blockchain at a specific time. Imagine you don't want to allow transactions from happening on your dApp from 12 am-2 am. To do this the smart contract needs to be able to store and save time within itself. Solidity uses something called the Unix Epoch Timestamp. This Epoch is basically how Solidity stores and saves time so that you can use it to manipulate the Blockchain. Time in Solidity is represented in an integer as the number of seconds that have passed since the Epoch Timestamp. Solidity has provided uses a method to access this time as per our requirement by using 'block. timestamp' which can also be called by a key called 'now'. Both of these do the same thing, i.e tell the time.

Let's take a look at the syntax to see how 'now' works:

pragma solidity ^0.8.5;
 contract Time { 
uint256 public createTime; 
function Time() public {
 createTime = now; 
  }
}

As you can see we are calling now. This means that it will return the time in seconds since the Epoch Timestamp as to when to transaction was mined.

If you have any questions, you can find me on my Twitter and LinkedIn