How to use Time in a Smart Contract?

Ajith M
Coinmonks
1 min readMar 10, 2022

--

While writing a Smart Contract you might come across a time-based requirement. In such a situation, you should be able to store time in the Smart Contract.

So, how to store time in a Smart Contract?

In solidity, we use integer to store time in Unix time format.

Take a look at the below Smart Contract.

Points to be noted:

  • We are using the type unit (unsigned integer) to store the Unix timestamp.
  • In solidity, ‘block.timestamp’ gives the current time. We can verify this by calling the ‘getCurrentTime()’.
  • We can also perform different actions such as ‘==’, ‘≤’, ‘≥’ with the Unix time format.

Thanks for reading this article ❤

To learn more about Blockchain and Smart Contracts, visit ontheether.com.

Comment on the topic of Blockchain you would like to learn next.

Clap 👏 If this article helps you.

Connect with me on LinkedIn and GitHub.

--

--