Why do programmers format dates using YYYY-MM-DD?

Because a dictionary sort puts them into the correct order.

Naming documents

We often create multiple documents with the same purpose but for different times.

  • Newsletter 29th October 2010
  • Newsletter 5th February 2011
  • Newsletter 15th April 2011
  • Newsletter 1st July 2011

It is usually good to give such documents a name that includes the date.

Dictionary sorting: the problem

However, when we look at the list of documents, something terrible happens.  On older computers, they will usually be ordered like this:

  • Newsletter 15th April 2011
  • Newsletter 1st July 2011
  • Newsletter 29th October 2010
  • Newsletter 5th February 2011

What went wrong?

The computer sorted like a dictionary.  Compare the first letters.  If they are identical, proceed to the next letter.  Keep moving forward until the letters are different.  If you reach the end of the text first, sort by length.

When comparing letters and numerals, computers usually sort any digit ahead of any letter – so “0”, “1” and “9” all sort ahead of “a” and “z”.  So, the computer sorted “15th” ahead of “1st”, because it considers “5” to be less than “s”.

Numeric sorting: still a problem

Newer computers and operating systems are slightly smarter.  They detect numbers, and sort those numbers in numeric order, not dictionary order.  This is better, but doesn’t sort our newsletters properly:

  • Newsletter 1st July 2011
  • Newsletter 5th February 2011
  • Newsletter 15th April 2011
  • Newsletter 29th October 2010

The computer detected the numbers, but didn’t notice they were part of a date.  It just sorted “1”, “5”, “15” and “29”.

YYYY-MM-DD: the solution

We could wait until computers get smarter still, so they recognise dates and sort accordingly.  The only problem then will be ambiguous dates like “01/02/2011”, which some people think means “First of February 2011”, and other people think means “Second of January 2011”.  Also, two-digit years will remain ambiguous.

Or, we could name our documents so that both dictionary and numeric sorting will put them into the correct order.  To achieve this, put the digits in order of significance.  The year goes before the month, and the month goes before the day.  Use leading zeroes to prevent the length changing.

The result?  YYYY-MM-DD.

  • Newsletter 2010-10-29
  • Newsletter 2011-02-05
  • Newsletter 2011-04-15
  • Newsletter 2011-07-01

Why use dashes instead of slashes?

It might seem logical to continue using the “/” symbol, so the date is formatted “YYYY/MM/DD”.  This has two problems:

  1. If we name a document on our computer, that becomes the name of a file.  The “/” is commonly used to separate folder and file names.  Windows does not permit the “/” in filenames, even though its own filesystem uses “\” as a separator instead.  UNIX (including Mac OS) allows them, but they still cause confusion and should be avoided.
  2. This risks creating a third type of ambiguous date if the year is ever abbreviated to two digits.  Which day is “10/11/12”?

Why not just sort by Date Modified?

Sometimes, it’s easier to sort by the date a document changed.  However, this date is not always accurate:

  1. A document may be changed before or even after the date it applies to.
  2. If you receive a file as an e-mail attachment, or download it from a website, the date it changed is the date it was retrieved.
  3. Battery-powered devices may lose track of the time when their batteries are changed.  It is common to find that photos from a digital camera were last modified on the 1st of January of the year the camera was built.

Should I name my documents like this too?

If you have documents that link to (not embed) other documents, don’t do this.  You will break the links.

If your documents are shared within an organization, talk to your co-workers before renaming everything in sight.

If you are at home, ask yourself: can you find your files from last year?  How about five years ago?  Would using names like these make it easier?

Which should I put first, the name or the date?

The item that appears first will have sorting precedence.  You will have to decide what makes more sense.  For most purposes I put the name, then the date.  This means everything beginning with “Newsletter” is grouped together after sorting.

What if I sort by both at different times?

Maybe you sometimes need to group a folder full of documents according to purpose, and sometimes need to put the same set of documents into date order.  A single list with a single sorting method can no longer help.  You need a table, where you can sort any column.  You need to be maintaining a spreadsheet, or possibly even a database.

YYYY-MM-DD-HHMM: adding the time

If you have multiple documents relating to the same date but different times, continue adding digits in order of decreasing significance (hours before minutes, minutes before seconds, and so on).  Use twenty-four hour time, not AM/PM.

The Y10K bug

In just under 8,000 years, this naming scheme will no longer work.  In the unlikely event of my files still being relevant by then, computers will hopefully be smart enough to sort correctly regardless.

Leave a Reply

Your e-mail address will not be published or shared with third parties.

First comments from new name/e-mail combinations will be held in moderation.