Trekk

Is it possible to get a block by timestamp?


I use Bitcoin Core API and I can retrieve a block by header hash as parameter using the getblock method. I need to get a block by given timestamp (median time).
I found some solution.

difference_in_blocks = round((last_block_mediantime — given_timestamp) / 600sec)
desired_block = last_block_heigth + 1 — difference_in_blocks

600 seconds — average time of a block mining (I assume this value is a constant)

Is there a specific API method or a better, more accurate way to do this, than my solution?


Article source: “https://bitcoin.stackexchange.com/questions/85315/is-it-possible-to-get-a-block-by-timestamp”