I want to create a difficulty calculator and need to know when each 2016th block has been created and how many blocks were created in the previous two weeks.

Do I have to count and label each block from the genesis block up manually or is there an easier way without having to download and parse the entire blockchain or use an external service?

2 Answers 2


To find the height of a block with only the raw blockchain (or block header) data, you need to follow through the sequence of references all the way to the genesis block.

Of course, there are external services offering this information in their API.

As for your premise, to calculate difficulty you don't need to know how many blocks were created in two weeks, only how much time passed for 2016 blocks (or actually 2015 blocks due to an off-by-one bug).


I use bitcoin Abe (MySQL DB) to parse the block chain so I can import it into Matlab for analytics. There is what appears to be a network attack around block_id 225503. To get around this and other orphan blocks, I started in the block table and queried SELECT block_id,block_height,block_ntime,block_total_satoshis,prev_block_id FROM abe.block WHERE block_id=" block# " I then used prev_block_id and started at the most recent block in the block chain and worked iteratively backwards using the prev_block_id to pick the next block_id.

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.