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
max taldykin
  • 2,966
  • 19
  • 27
Toshi
  • 2,357
  • 8
  • 23
  • 34

3 Answers3

20

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

chainStartTime = block.timestamp;

Previously, there was now, but it has been deprecated

Serj Sagan
  • 429
  • 1
  • 6
  • 14
  • 6
    "now" has been deprecated. Use "block.timestamp" instead. – Mahmoud Sep 12 '21 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 Sep 01 '22 at 15:31
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

euri10
  • 4,640
  • 5
  • 24
  • 55
  • 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. – BokkyPooBah Apr 15 '16 at 15:56
  • @BokkyPooBah I assume this means there is no way of getting current time in solidity (yet) ? – Teleporting Goat Oct 28 '16 at 12:18
  • 2
    @TeleportingGoat In your solidity contract code you are able to access the block.timestamp - see http://ethereum.stackexchange.com/questions/413/can-a-contract-safely-rely-on-block-timestamp/428#428 – BokkyPooBah Oct 28 '16 at 12:36
  • Is this library workable for polygon as well??? – Abdullah Hanif Chaudhary Jun 20 '22 at 07:37
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.

Nick Fitz
  • 21
  • 2