I was reading about the Italian invasion of France earlier, and apparently the Italians gave a solid 7 hours of warning before the war took effect, and I've heard about it happening before, but not recently.
Why did nations do this? Why not just preemptively strike?
Reports of declarations of war go back all the way to zncient Egypt, although I have very little knowledge and sources...
In Ancient, Medieval and Renaissance eras, declarations of war were often made with a certain goal in mind. In wars of succession, for example, such a declaration could act as a way for claimants to make their cause more well known, possibly to rally supporters to their banners or to undermine the claim of other claimants. In other cases, city states declared war for political reasons, with border disputes as a nice example.
Declaring war gave the defending party the chance to surrender to the demands of the aggressor, preventing bloodshed and the general cost of war
I think the key factor to remember is that war itself is rarely the goal of a war: why go through an entire war, costing tons of men, materiel, gold, supplies, etc if you can scare the other party into submitting to demands?
The Hague Conference (1917) required an official declaration of hostilities between nations before any agressive action could be undertaken.
The UN Charter (1945) forbids all agressive actions between members and thus makes declarations of war exceedingly rare.
Some post 1945 examples include Egypt, Jordan, Syria, Iraq ánd Lebanon declaring war on Israël (1948), Somalia declaring war on Ethiopia (1977) and Chad declaring war on Sudan (2005).
As an interesting sidenote, it's become more popular for countries to declare "the existence of a state of war", which is something like a declaration after hostilities have started.
Interesting examples include Panama declaring this against the USA (1989), Argentina vs the UK (the Falklands War, 1982), Georgia vs Russia (2008) and most recently Egypt declared the existance if a state of war with Islamic State in 2012.
Hope this helps :)