18

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)

3 Answers 3

22

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.

2
  • 1
    I have so much questions. I would really appreciate if you answer some of them. First of all why are you talking about transactions? How transactions is related to contracts? How miners deploy contract code to blockchain and how it is stored there? How it differs from transactions? What do you mean by time set in the future? If you call now in one of your contract methods would it show the time-stamp of the block on which code of contract reside in, am i getting this right? Thank you very much for your time and wisdom)
    – Piliponful
    Commented Jun 18, 2017 at 7:11
  • 2
    A contract defines some code. The code consists of methods. This code doesn't do anything except when it is called. A call is part of a transaction. A transaction is part of a block. If you write a contract with a function that logs now aka block.timestamp, each time it is called by a transaction it will log the time declared in the block containing the transaction. This will be approximately the time the miner mined the block, but they can fudge it a bit. Commented Jun 18, 2017 at 8:45
6

No, In solidty there is no Date object which will give you the actual time, we can only get the timestamp of the block in which the contract is invoked, which is deterministic. You can use now keyword or block.timestamp in solidity to get the timestamp of the current block. This can be used to work with time. Reference to an auction contract is here.

2

Try https://github.com/pipermerriam/ethereum-datetime. It works with a unix-style uint timestamp under the hood. You get a DateTime object with year, month, day, hour, minute, second and even the weekday.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.