< Index

Use ISO-8601 Date Format

Daily, lots of people have trouble with date formats. Obviously, there's the issue of different countries having different formats - like dd.mm.yyyy vs mm/dd/yyyy, but that isn't all. Some people only use 2 digits per date part - good luck deciphering a date like 07.05 or 07/05! Is it the day and month, the month and day, the month and year, the year and month? You can't tell! Best case you can guess based on surrounding context.

Even ignoring the confusion issues, what about file names? If you put a date before the filename, and the date is written in one of the two common date formats, then sort it alphabetically, the files will be sorted in a fairly awkward order, certainly not chronologically.

But how can we solve these issues? Surely, no matter the date format, there's a potential to confuse it?

Wrong! It's possible to design a date format that's not confusing, while having alphabetic sort be the same as chronological sort, and the format is specified in the ISO-8601 standard.

To cut to the chase, the format itself is yyyy-mm-ddThh:mm:ss+-timezone

For example, 2022-04-11T00:29:15+07, the current time as of writing this sentence. You could also write the timezone as 07:00 or 0700, and the date as 20220411, and the time as 00 or 00:29 or 00:29:15.000 or 002915 and so on, but the core part stays the same. GMT timezone can be written simply as Z (instead of +00 or -00).

The "T" in the middle is fairly controversial, and is there to make it easier to figure out something is a time. There could be other ways to solve it, such as making the - and : signs mandatory, or just replacing T with _ or a space to make it more humanly readable. I'm not asking you to religiously follow ISO-8601 to the letter, I'm simply asking you to use it to the extent possible to minimize the ambiguousness! As a bonus, every mainstream language has ISO-8601 parsing and serialization either built in or easily available in a library. Conversely, if a language pushes a different time format, you should seriously doubt the competency of the developers... just kidding... or am I?

The important part is the format is easier to understand for all kinds of people - those who are used to dd.mm.yyyy will be used to the parts being ordered by significance, and will naturally assume it's yyyy-mm-dd, not yyyy-dd-mm, while those used to mm/dd/yyyy will be used to month being written before day, and hence yyyy-mm-dd should come naturally to them too. Hopefully nobody will write yy-mm-dd and further complicate the matters to those reading ISO-8601 for the first time!

ISO-8601 also has some weirder parts, such as defining how to specify week numbers in a year (and people actually use it!), or durations, or intervals. For details, refer to the standard itself... or just read Wikipedia.

Many countries actually adopted ISO-8601 as their national date format! Hence, you can set your datetime locale to a country like Denmark to have locale-respecting apps use ISO-8601.

On Linux, this is done by setting LC_TIME to en_DK.UTF-8. For example, do this if you use NixOS:

i18n.extraLocaleSettings.LC_TIME = "en_DK.UTF-8";

Have any comments, questions, feedback? You can click here to leave it, publicly or privately!