23

I'd like to describe time like Apr15th 10:00AM . I know how to describe time range as follows, but I am not sure how to call date of time. Could you tell me about it if you know?

1 == 1 second
1 minutes == 60 seconds
1 hours == 60 minutes
1 days == 24 hours
1 weeks = 7 days
1 years = 365 days

3 Answers 3

19

15 April 2016 10:00 UTC translates to 1460714400 in Unix Timestamp.

I never used it but there is this library by @pipermerriam called ethereum-datetime, so you could call his contract to get what you want

4
  • 2
    Note that the ethereum-datetime does not provide the smart contract with the current datetime. ethereum-date provides you with the ability to format the time instead of displaying the number of seconds since 01/01/1970. Commented Apr 15, 2016 at 15:56
  • @BokkyPooBah I assume this means there is no way of getting current time in solidity (yet) ? Commented Oct 28, 2016 at 12:18
  • 2
    @TeleportingGoat In your solidity contract code you are able to access the block.timestamp - see ethereum.stackexchange.com/questions/413/… Commented Oct 28, 2016 at 12:36
  • Is this library workable for polygon as well??? Commented Jun 20, 2022 at 7:37
20

I believe what you're looking for is block.timestamp:

chainStartTime = block.timestamp;

Previously, there was now, but it has been deprecated

2
  • 6
    "now" has been deprecated. Use "block.timestamp" instead.
    – Mahmoud
    Commented Sep 12, 2021 at 23:56
  • There are several workarounds. The first one is based on the block's timestamp: each timestamp should be bigger than the parent's one, although nodes can manipulate timestamp especially when it promises profits to them, also blocks could be rejected during mining. The second one is based on using the block’s number as they are ordered one-by-one and by knowing average block time execution calculate current time - unfortunately the time of block mining also might vary, especially with new ethereum updates. Seems like the timestamp of the block is the better option.
    – Gleichmut
    Commented Sep 1, 2022 at 15:31
2

block.timestamp does not give the current time, as you might expect a clock on the wall to. In a mined block, block.timestamp is the local time of the miner's machine when the block was mined. For live transactions running in a yet to be mined block, the block.timestamp is the timestamp of the most recently mined block. It will be reset to the time of the miner's local clock when that block gets mined. To get a live time feed you have to go to an external source.

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.