James Bachini

Timestamp in Solidity | Solidity Tips & Examples

timestamps solidity

A timestamp is a numerical value that represents a specific moment in time. It’s often used to track when a certain event occurred or when a piece of data was created or modified.

The most common way to represent a timestamp is by using the number of seconds that have elapsed since a specific moment in time, in Solidity and most other languages this is January 1st, 1970, at midnight (UTC), also known as the Unix epoch.

To obtain a timestamp in Solidity we can use a global variable that returns the number of seconds that have elapsed since epoch time.

uint secondsPassed = block.timestamp;

The block.timestamp property in Solidity returns the elapsed time in seconds between the Unix epoch and the moment the block was validated. Note that this value is somewhat manipulatable, to an extent, because the validator node decides the exact moment when the block was added, which determines the value of the block.timestamp value.

Solidity also accepts some time units such as seconds, minutes, hours, days, and weeks, which are converted to their respective value in seconds.

uint oneYearInSeconds = 365 days;
Solidity Timestamp

Get The Blockchain Sector Newsletter, binge the YouTube channel and connect with me on Twitter

The Blockchain Sector newsletter goes out a few times a month when there is breaking news or interesting developments to discuss. All the content I produce is free, if you’d like to help please share this content on social media.

Thank you.

James Bachini

Disclaimer: Not a financial advisor, not financial advice. The content I create is to document my journey and for educational and entertainment purposes only. It is not under any circumstances investment advice. I am not an investment or trading professional and am learning myself while still making plenty of mistakes along the way. Any code published is experimental and not production ready to be used for financial transactions. Do your own research and do not play with funds you do not want to lose.