How to write a date seems kind of trivial for a technical tip, but it is obviously needed because so many people get it wrong.
Your date format is especially important when you use it for versioning by putting it put a date at the end of your filename.
The ISO 8601 standard says that dates should be in the format yyyy-mm-dd. You can stop reading now if you promise to use this format for your dates in the future…
…still with us? You obviously need more convincing.
The two main problems with dates are ambiguity of format, and sorting/comparison.
Try to find the most recent date in this (sorted) list:
Tricky? First of all, the list is sorted alphabetically, so you have to read the entire list instead of just picking the last date in the list. Secondly, the date format is ambiguous; is it using the American date format (mm-dd-yyyy) or the European date format (dd-mm-yyyy)? The last value in the list gives you a clue that it is using the European date format, where day appears first. So the most recent date is item 2 – 1st of October, 2009.
Here is the same (sorted) list, this time using the ISO 8601 format:
See how much easier it is to find the correct date?
The same concept applies to code you may have to write. It is simple to use strcmp() to determine which date occurs later if the dates are in a sensible format. The same exercise becomes painful when dates are in dd-mm-yyyy or mm-dd-yyyy format. Even worse are dates that use the name of the month instead of a number.
Using a sensible date format is such an easy thing to do. If you are storing a date in a computer, please help make everyone’s life (slightly) easier and use yyyy-mm-dd.