Meni's answer is good. I just want to give some practical detail method about difficulty calculation, perhaps helpful for future views of this question's answer.

Let's take a look at Satoshi's genesis block header (part of related info):

```
$ bitcoin-cli getblockhash 0
000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
$ bitcoin-cli getblockheader 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
{
...
"height": 0,
...
"bits": "1d00ffff",
"difficulty": 1,
...
}
```

As we can see above, the genesis block has a '1' difficulty and '1d00ffff' bits. The bitcoin bits means the 'target' hash value, the new generated block must meet a condition: block header's double SHA-256 hash value must less than this 'target' value.

The '1d00ffff' bits value in genesis block means the 'target' value:

```
[0x00000000,0xffff,{0x00..0x00}]
{0x00..0x00} at above has 26 bytes 0x00.
```

Then, to find a new block, you must search that 32 bits nNonce value (and nTimes and the hashMerkleRoot also) until the block hash value has 4 bytes zero leading.
By the way, the nNonce is one of the fields in block header structure:

```
struct header_structure{ // BYTES NAME
uint32_t nVersion; // 4 version
uint8_t hashPrevBlock[32]; // 32 previous block header hash
uint8_t hashMerkleRoot[32]; // 32 merkle root hash
uint32_t nTime; // 4 time
uint32_t nBits; // 4 target
uint32_t nNonce; // 4 nonce
};
```

Because SHA-256 algorithm (as well as any cryptographically secure hash algorithm) produces output that will appear like an uniformly random sequence, the practical 'trial and error' method is the only way to find a new block to meet the condition. The probability to find a block with the 4 bytes zero leading hash value is 1/(2^32), that means the average 'trial and error" numbers are exactly 2^32 (i.e. 4G).

For human easy understanding about this 'target' hash value, We define the term 'difficulty', which means the average 'trial and error" numbers to find a block to meet the 'target' condition. And we define the 'difficulty' unit:
1 'difficulty' = 4G hashes

Then, till today, the bitcoin blockchain height reach 501509, let's take a look at its header:

```
$ bitcoin-cli getblockheader 0000000000000000006c5532f4fd9ee03e07f94df165c556b89c495e97680147
{
...
"height": 501509,
...
"bits": "18009645",
"difficulty": 1873105475221.611,
...
}
```

The block 501509's bits = 0x18009645, it's the compact format of 256 bits integer, its 256 bits format is:

```
[0x00000000,0x00000000,0x009645,{0x00..0x00}]
{0x00..0x00} at above has 21 bytes 0x00.
that is 0x009645 * (256 ^ 21）
The genesis block's target is ( 0x00ffff * 256 ^ 26 ）which is the difficulty unit '1.0'.
So, the difficulty
= (0x00ffff * 256 ^ 26）/ (0x009645 * 256 ^ 21)
= 65535/38469 * (256^5)
= 1.703579505575918 * 2^40
= 1873105475221.611
```

So far, you have all the detail about how to calculate the 'difficulty'.
In some cases, we also use the simple format **1.7T** to say the difficulty, in above example：

```
(1.703579505575918 * 2^40) = 1.703579505575918T
1T = 2^40 = 1024^4
```

2I think follow up questions are better as comments to the answer. Basically yes, but no summing is actually needed - you can just take the timestamps of the last block and of the one 2016 blocks before, and subtract. – Meni Rosenfeld – 2012-12-19T14:18:30.203