How to use dates/times in Solidity
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!
Next post
π Solidity Auditing online quiz
Learn how to audit smart contracts by looking at some example code and trying to find the bugs
β½ Solidity Gas Optimizations Guide
How to optimize and reduce gas usage in your smart contracts in Solidity
π§ͺ Guide to testing with Foundry
Guide to adding testing for your Solidity contracts, using the Foundry and Forge tools
π Guide to UTXO
UTXO and the UTXO set (used by blockchains such as Bitcoin) explained
π Solidity Assembly Guide
Introduction guide to using assembly in your Solidity smart contracts
π¦ Ethereum EOF format explained
Information explaining what the upcoming Ethereum EOF format is all about