Skip to main content
Share Your Experience: Take the 2024 Developer Survey
59 votes
Accepted

Is the block.timestamp value in Solidity seconds or milliseconds?

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 ...
Richard Horrocks's user avatar
27 votes
Accepted

How do Ethereum mining nodes maintain a time consistent with the network?

Ethereum nodes (regardless of mining) need to have an accurate time, otherwise they will not be able to connect to peers and to the network (https://github.com/ethereum/go-ethereum/wiki/Connecting-to-...
eth's user avatar
  • 85.9k
27 votes
Accepted

Is block.timestamp safe for longer time periods?

Yes, you can guarantee long run time accuracy. Miners can't cheat that much to change the timestamp over days. Blocktime is adjusted to be constant within certain error margins, and timestamps is ...
Roland Kofler's user avatar
25 votes
Accepted

How to get the current time in Solidity

Within the Ethereum Virtual Machine, there isn't a way to get the current time, other than the block time. So there is no way to get it in Solidity. Why? There is disagreement between computers ...
carver's user avatar
  • 6,381
24 votes
Accepted

Solidity: Is there a way to get the timestamp of a transaction that executed?

A transaction doesn't have a timestamp. However, every block provides a timestamp (when it was collected), but if the time is critical for you, don't refer to this timestamp because a miner could ...
Badr Bellaj's user avatar
  • 18.8k
22 votes
Accepted

How do you work with Date and time on Ethereum platform

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....
Edmund Edgar's user avatar
  • 16.9k
20 votes

How to get current time by solidity?

I believe what you're looking for is block.timestamp: chainStartTime = block.timestamp; Previously, there was now, but it has been deprecated
Serj Sagan's user avatar
19 votes
Accepted

How to get current time by solidity?

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 ...
euri10's user avatar
  • 4,640
18 votes

Is the block.timestamp value in Solidity seconds or milliseconds?

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) { ... } } ...
eth's user avatar
  • 85.9k
17 votes

Is block.timestamp safe for longer time periods?

The miner could cheat in the timestamp by a tolerance of 900 seconds. so if you check outside this intervall you are safe.
Badr Bellaj's user avatar
  • 18.8k
17 votes
Accepted

Is Solidity block.number more secure than timestamp?

The block number will always be correct by definition: It's number x in the chain, because it's chained on top of x-1. However, as you say block.timestamp can be gamed a little bit - or with the ...
Edmund Edgar's user avatar
  • 16.9k
12 votes

Solidity: Timestamp dependency, is it possible to do safely?

Just to elaborate on Dennis' answer and explain why this hindrance exists. Deep down, the decentralized nature of the consensus means that no one's clock is any better than anyone else'. Blocks ...
Rob Hitchens's user avatar
  • 55.3k
11 votes
Accepted

Block time and confirmation time

Block time is the expected interval between two blocks. For ethereum, this is around 15 seconds. For Bitcoin, it is 10 minutes. The number of confirmations is simply the number of blocks that have ...
Raghav Sood's user avatar
  • 4,050
10 votes

How to get the block number which is the closest to a given timestamp?

DeFiLlama has a new API that does this. https://defillama.com/docs/api Just GET https://coins.llama.fi/block/ethereum/1658171864 You can also check different chains
cwatkins's user avatar
  • 201
9 votes

How do Ethereum mining nodes maintain a time consistent with the network?

Here's the code I've found so far that deals with syncing time. It uses pool.ntp.org:123 as the time syncing source. From Go Ethereum - p2p/discover/ntp.go, lines 48-65: func checkClockDrift() { ...
BokkyPooBah's user avatar
  • 40.3k
9 votes
Accepted

Can a child block have an earlier timestamp than the parent block?

block_timestamp CANNOT be before parent_timestamp This is enforced by the protocol, see yellow paper: Hs is the timestamp of block H and must fulfil the relation: (48) Hs > P(H)Hs Also in my ...
Roland Kofler's user avatar
9 votes

Solidity: Timestamp dependency, is it possible to do safely?

It depends on how fine-grained you need to be. The timestamp has to have a later time than the previous block's timestamp, and if it's too inaccurate then other miners will reject it. So if your ...
Dennis Peterson's user avatar
8 votes

Calculate approximative block height at a certain time?

If you just mean a general formula, rather than something written in Solidity... Something along the lines of: last_block_number + ((future_time - time_now) / block_time) Where the future_time and ...
Richard Horrocks's user avatar
8 votes

How does Ethereum regulate the time between blocks?

If you are using geth here's a patch that I use to accelerate mining. You could probably modify it to decelerate mining if you wish. (I'm curious why you wish to go slower). In any event, I think ...
Paul S's user avatar
  • 4,271
8 votes
Accepted

How does CryptoKitties track time?

now is a Solidity special variable, which equates to the current time since the epoch, in seconds. From the documentation (linked above): now (uint): current block timestamp (alias for block....
Richard Horrocks's user avatar
8 votes
Accepted

Understanding ganache timestamps

I wrote an entire article on this: Writing Accurate Time-Dependent Truffle Tests TL;DR The 1 second comes from run time costs, i.e. the time it takes for the javascript promises to resolve. For ...
Paul Razvan Berg's user avatar
7 votes
Accepted

Can I retrieve the timestamp the contract was mined on, inside of the contract?

You have to code it into your contract either by saving the block.timestamp in the constructor: contract A { uint public createdTimestamp; function A() { createdTimestamp = block.timestamp; ...
Denis's user avatar
  • 911
7 votes
Accepted

What recommended amount of storage for storing timestamps?

In the Yellow Paper the block's timestamp is storead as a scalar integer less than 2256. If you do not care about space then an uint256 should be good enough for your future needs. Ethereum uses the ...
Ismael's user avatar
  • 30.6k
7 votes
Accepted

How to get the block number which is the closest to a given timestamp?

Ethfinex just published a function that does exactly that. It's to "pixel perfect" but it's working really well so far, you give it a timestamp and it will walk the blockchain backwards until it ...
kroe's user avatar
  • 226
7 votes

Miner-modifiability of block timestamp after the Merge

Does that mean block timestamps can no longer be tweaked or fudged by miners, after the Merge? That's correct, (post-Merge) consensus on valid blocks is pre-determined timestamps that are not ...
eth's user avatar
  • 85.9k
6 votes

Is the block.timestamp value in Solidity seconds or milliseconds?

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. ...
Vikram's user avatar
  • 181
6 votes

How do you work with Date and time on Ethereum platform

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 ...
joifsi's user avatar
  • 1,019
6 votes

Is the block.timestamp value in Solidity seconds or milliseconds?

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 ...
Badr Bellaj's user avatar
  • 18.8k
6 votes
Accepted

Is block's timestamp current block's or previous block's timestamp?

It's the timestamp of the current block. The idea is like this: When a miner mines a block, he/she picks the timestamp, executes all contracts with the chosen timestamp to update the state, and does ...
Loi.Luu's user avatar
  • 2,103
6 votes

How do Ethereum mining nodes maintain a time consistent with the network?

TL;DR: Blocks must be within reasonable Unix time or they will be rejected. The yellow paper states: timestamp: A scalar value equal to the reasonable output of Unix’s time() at this block’s ...
Roland Kofler's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible