Dating ancient texts is often a hard task for historians: they did not have any uniform scheme of dating, often relying on regnal years, eponyms and similar instruments which are notoriously hard for us to decode. In fact, where there's no external evidence, datings of a single document or event can differ by hundreds of years.
The Bible adds even more difficulties here: its texts are often undated, many of them are redacted from earlier ones, but with significant additions by redactors, some purport to have been written when they could not have been, at least in their received form.
I already mentioned in the first part that Goliath's description in 1 Sam 17 fits more a 7th century Greek mercenary than a 10th century Philistine warrior (we don't really know who Philistines were, BTW). This means that this text could not have taken its final shape before the 7th century. But it does not mean that the story of David and Goliath is fantasy, it could after all be based on earlier sources with the 7th century redactor describing Goliath as what was a stereotypical enemy then.
In general, when dating ancient events and text we strive to determine time limits which are usually known as terminus post quem, that is limit after which, the date when the event had happened at the earliest (or the text had been written), and terminus ante quem, the date when it had happened at the latest. (There are also limits terminus ad quem and terminus a quo which are used for periods rather than events.) Obviously, the closer one is to the other, the better, if they are equal then we have a precise dating. Which is unfortunately very rare in ancient history.
Among various types of evidence that helps establish datings one very interesting (especially in the Bible) is prophecy (in the narrow sense of foreknowledge of future events). In general, historians are expected not to believe in God or at the very least so to behave. That is, the religious explanation of a prophecy: God had revealed to the prophet that some event is going to happen in the future is off limits to a historian. They must look for another explanation. Which brings us to the first, and probably most helpful kind of prophecies.
Vaticinium ex eventu
Prophecy from the event, that is a prophecy which prophesies an event which has already happened, a kind of pious fraud, if you will, yet there are numerous instances in the Bible.
Since the prophet knows events which are ostensibly yet to happen and the divine explanation is off limits, the rational one is that the prophet is, in fact, writing after the event had already happened. This is mostly visible in the Book of Daniel which describes events leading to the sack of the Temple in Jerusalem (167 BC) quite accurately, yet for later events is either vague or simply wrong (it prophesies Antiochus' death in Palestine, yet he died in Persia in 164 BC).
This leads us to the conclusion that the writer (at least of this fragment, there are more in Daniel) had completed his account somewhere between 167 and 163, and not knowing the future he tried to tell future events, make them up even, without impressive results. As for what the prohpet got right, we assume then that he simply knew the events because they had already happened, which gives us terminus post quem of 167 BC.
It is however important to avoid being overly enthusiastic with this idea and be wary of events which could be easily predicted without divine intervention. This would be the case e.g. with Jeremiah's prophecies: it was not very difficult to figure that if Judah doesn't surrender to Babylonia, it will be defeated and destroyed. This Nebuchadnezzar guy just wasn't the one to make a fool of and one didn't need God to know it.
Sometimes a lucky guess may also happen, one of the best examples is (from nearly our times) marshal Ferdinand Foch's comment on the Versailles Treaty: This is not a peace. It is an armistice for twenty years. As we know the WWII started twenty years later. Foch could not know this beforehand, or say after the event (he died in 1929), to stress his point he could use another number, fifteen or perhaps thirty, he said twenty. Just a lucky guess. We have evidence enough to know that this was the case, and even if we didn't a guess would still be a probable explanation, but vaticinium ex eventu would be a tempting one.
Unfulfilled prophecies
Just as a fulfilled prophecy may be useful in establishing the terminus post quem, an unfulfilled one will help to establish the terminus ante quem: if an event that was predicted by a prophetic text didn't happen when expected or another event happened that made the first one impossible to happen, and this other event is securely dated, it means that the prophet didn't know what was future for him and made a false guess or perhaps he misunderstood the divine revelation. Either way, he said it before, or he'd make a fool of himself.
This is also applicable to the prophecies in the Book of Daniel, above all to the one predicting Antiochus' death in Palestine. We know quite securely that Antiochus died in 164 BC in Persia, not in Palestine, so it's pretty clear that these verses at least started circulating in their final form before news of Antiochus' death could reach the prophet, which would mean somewhere in 163 BC.
Again, it is not always clear whether a prophecy can be so used to date an event, or even whether it was fulfilled. For example, in Mark 13:2 Jesus predicts the destruction of the Temple so that Not one stone will be left here upon another. The Temple was destroyed but not so completely, in fact the Western Wall still stands today, nearly two thousand years later. So this would date the Gospel of Mark to before AD 70 as an unfulfilled prophecy, wouldn't it?
Not so fast. This still may be a vaticinium ex eventu, even if it's not literally true. It is important to remember that the Evangelist would not necessarily visit Jerusalem and see the ruins with his own eyes. It is more probable that he'd know about the event from relations of refugees, maybe even second hand, and this kind of event gets easily exaggerrated in oral transmission. Thus, he could have thought that no stone remained one [...] upon another, and so write in his Gospel.
In general, a clear unfulfilled prophecy is quite rare. Prophets (or God) knew that future telling is a risky business and tended to prefer highly symbolic visions to precise predictions of future events. Which is perhaps not surprising, but not helpful either.
Conclusion
There are various difficulties, often unsurmountable ones with dating ancient, and especially Biblical texts. In the case of most Old Testament ones - and this does not mean books as these are often composed from various earlier texts, but often verses - it's even harder than usual. Sometimes a prophecy which is too good to be true, at least beforehand, or otherwise a bad one is useful to limit possible dates of text completion.
This is of course only one method of dating texts, and not the most important one, there are several other that are used in dating Biblical texts.
This is the second text in the series about using the Bible as historical evidence. I plan to publish texts that are longer and have more references to source material and publications than this, but so far for technical reasons I am unable to structure them as I would like to. Thus, at least until this is resolved, I will be publishing more (unplanned) introductory articles. I hope not for very long.