New to blockchain software development? Read my beginners guide here

How to use dates/times in Solidity

Created on August 2022 β€’ Tags: ethereumsolidityguides

A guide to using times in Solidity


Table of Contents for How to use dates/times in Solidity


now

The now constant will return the unix timestamp of the current block

function getTimestamp() public returns (uint) {
return now; // num of secs since 1970-01-01
}

Units (minutes, hours, days, weeks, years)

You can use the units of minutes, hours, days, weeks, years in Solidity.

uint anHour = 60 minutes; // 60 * 60
uint halfADay = 12 * hours; // 12 * 60 * 60

Comparing dates/times in Solidity

As the units mentioned above are just units, you can use > and < like any other number.

Difference between two dates/times in Solidity

As the units mentioned above are just uints, you can use - and + to run normal math operations on them, including finding the difference with -.

Notes:

  • these do not take into account num of days in a month or leap years/leap seconds

Also see how to use Ether values in Solidity

See using ether values in Solidity to use things like 1 ether or 50 wei.

Spotted a typo or have a suggestion to make this crypto dev article better? Please let me know!

See all posts (70+ more)

See all posts (70+ more)

Was this post helpful? πŸ“§

If you liked this content and want to receive emails about future posts like this, enter your email. I'll never spam you.

Or follow me on @CryptoGuide_Dev on twitter

By using this site, you agree that you have read and understand its Privacy Policy and Terms of Use.
Use any information on this site at your own risk, I take no responsibility for the accuracy of safety of the information on this site.