Formatting dates

It's well-known that different countries use different date formats. A funny thing is that the format used in the USA is even different from the format used in the UK.

There is a huge list of date formats on Wikipedia. The format differences lead to obvious misunderstandings. In many countries, the slash character is used to separate years, months, and days. But the order of the fields varies. For that reason, it's impossible to differentiate between February 1st or January 2nd without knowing the country. Worse: this separator character cannot be used in file names on most computers, Windows will complain with the following message:

A file name can't contain any of the following characters: \ / : * ? " < > |

What is not well-known is that ISO 8601 has been teaching us how to format dates since 1998! That document defines a standard way to format dates. Most people didn't hear about it, even if most of us already saw those dates somewhere, on a receipt, on a website, or in a file.

The dates below are compliant with that International Standard:

In essence:


Software-related notes

In most filesystems, a file has multiple properties such as a name, a creation date, an access date, and similar attributes. The creation date is not always a reliable information, as the file might have been copied across multiple computers, sent through emails or websites, and the recorded date may not be accurate. This is why photos often include additional metadata that contains the creation date. On the other hand, a file name is usually quite reliable, and we can embed the creation date in the file name.

To achieve that, one could simply use the prefix YYYY-MM-DD. The great outcome is that after sorting the files by name, the files will be grouped by year, month, day and then by name. Files from 2025 will appear after 2024, file from February will appear after January, and so on.

2025-02-22-MyFile1.docx
2025-02-23-MyFile2.docx
2025-02-24-MyFile3.docx

While it's sounds possible to manually name the files with the YYYY-MM-DD prefix, it does not sounds reasonable to manually name the files with a full ISO date. But a computer program could do that easily. We saw that digits are fixed for years, months, days and time. Because of this, the separators could be omitted. The date become less readable for an human, but can be stored in a filename.

20250218T050815Z-screenshot-in-local-time.png
20250218T144000+0100-screenshot-in-timezone.png

As of 2025, most of date software libraries will provide functions to interface with ISO dates, in JavaScript the function is named Date.toISOString:

const TimeNow = new Date().toISOString();
console.log(TimeNow);
-->