[Ethereum] How to work with Date and time on Ethereum platform

timerstimestamp

Is there a way to access Date object in solidity like in JS? If so, wouldn't it be wrong to call solidity deterministic? So i guess if it called so, you can't. So the question is – how do you work with time?
P.S. i would appreciate code snippets)

Best Answer

When a transaction is executed, it is done in the context of being part of a block. The block includes a timestamp (in seconds since 1970), which your contract code can refer to by the name block.timestamp.

The timestamp is set by the miner who mines the block and nobody can be sure at what time they really mined it, so it may not be exactly accurate. However, it must be later than the previous block, and other miners will tend to reject a time set in the future, so it can be relied on within reasonable tolerances.

Since what the network is validating for each transaction is not the current time at the point of validation but the time as declared by the miner who mined the block, the result is deterministic.

The JavaScript date object also provides functions like getting the day of the week or month of the year. These are not natively provided in the EVM, but you can write or use a library to help you with them. Often it is practical for the contract to deal only in timestamps, and for applications - which often consist of HTML/JavaScript pages interacting with Ethereum contracts - to use JavaScript functions to format them to display then to the user.

Related Topic