79

I'm looking to use block.timestamp in one of my contracts and I want to know whether the block.timestamp is seconds like in Python or milliseconds like in Java or some other value?

Also, is it safe to use block.timestamp to check if 30 days is past since the last updated time or something like that?

3
  • 2
    The blocks.timestamp is a Unix time stamp. So, it has the complete information about the date, hours, minutes, and seconds (in UTC) when the block was created.
    – galahad
    Aug 8, 2016 at 17:39
  • 2
    I thank you for your response. I very much appreciate it and for providing more details and clarifications.
    – etherfaces
    Aug 8, 2016 at 17:52
  • Please also see this post on the same subject: ethereum.stackexchange.com/questions/68064/…
    – djenning90
    Mar 12, 2019 at 21:00

4 Answers 4

59

From the Solidity documentation (here and here):

... timestamp of the current block in seconds since the epoch

For your other question:

Also, is it safe to use block.timestamp to check if 30 days is past since the last updated time or something like that?

Your question isn't completely clear, but assuming you're questioning the validity of a timestamp over a given period of time, then this previous answer should cover it: Is block.timestamp safe for longer time periods?

3
  • 1
    Thanks for pointing out the documentation. I some how missed it it is seconds. Let's say I set a timestamp value: lastUpdated in a previous block. In a future block, I check if the lastUpdated is past 30 days or not to do some action. Would this be a safe check? Assuming that the difference in time difference is reasonable(order of days) and not too close?
    – etherfaces
    Aug 8, 2016 at 17:42
  • 1
    30 days will work fine. It should be accurate to within a half hour or so at worst Nov 7, 2016 at 20:28
  • Worth noting that block.timestamp is only reliable for state-changing functions. NO guarantees that block.timestamp is meaningful for view functions.
    – GGizmos
    Nov 27, 2022 at 5:19
18

block.timestamp is a uint256 value in seconds since the epoch.

It's safe to compare like:

function f(uint start, uint daysAfter) {
    if (block.timestamp >= start + daysAfter * 1 days) { ... }
}

or

if (block.timestamp > start + 30 days) { ... }

Time units are helpful for some calculations with the caveat:

Take care if you perform calendar calculations using these units, because not every year equals 365 days and not even every day has 24 hours because of leap seconds. Due to the fact that leap seconds cannot be predicted, an exact calendar library has to be updated by an external oracle.

5
  • 1
    How do I define "start" variable. Should I just do: uint250 start = 1512918335 ?
    – FosAvance
    Dec 10, 2017 at 15:05
  • start = block.timestamp (now) or start = 23 days etc
    – Magno C
    Jun 20, 2021 at 4:36
  • It's not safe to compare in this way, especially for the logic you exhibited, where you're checking that at least a certain minimum amount of time has elapsed, since block.timestamp can be set arbitrarily far in the future, as long as it is greater than the parent timestamp: ethereum.stackexchange.com/a/428/82179 ethereum.stackexchange.com/a/432/82179 Nov 5, 2021 at 1:34
  • 1
    @LukeHutchison ethereum.stackexchange.com/questions/5927/… has some answers why block.timestamp doesn't end up being arbitrarily far in the future.
    – eth
    Nov 10, 2021 at 8:02
  • 1
    @eth looks like some implementations are much stricter than the specification, then. Thanks for the info. Nov 12, 2021 at 8:16
6

1-timestamp is the unix timestamp so to convert it to normal date use http://www.epochconverter.com/ it indicates when the block was created.

2-to answer if it is safer to use block.timestamp <30 days: yes because the miner could change the timestamp by 900 seconds.

1
  • Nice, using this way since I saw your comment. Let me ask you: In case I want to pass the days in the constructor, how can I do this? Do I need to specify days in seconds or its allowed to pass like "1 week" for example?
    – 0xTheL
    Sep 27, 2022 at 20:39
6

block.timestamp may not be the correct time as it is set by the miner. Hence depends on the accuracy of the miners clock.

The current block.timestamp has to be more than the parent block.timestamp. This is set in the protocol. Some blocks even have 1s difference.

Blockchain has no clock as it would mean sync of all the nodes and that would be almost impossible to achieve.

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.