What is your favorite date and time format in a file name?

mika picture mika · Aug 8, 2009 · Viewed 56.4k times · Source

This is a somewhat subjective question, and not very important in the big scheme of things, but something that yet annoys me regularly. There seems to be no self-evident way to put a timestamp in a file name.

Objective issue is that timestamps in file names should be sortable. But .NET sortable date formats like "s" ("yyyy-MM-ddTHH:mm:ss") and "u" ("yyyy-MM-dd HH:mm:ssZ") are not valid in file names because of ':' characters.

Other thing is that you should easily see if universal or local time is used. Practically, users seem to prefer local time to universal time.

I have mostly ended up using ISO 8601 with basic time format:

  • Local time format string "yyyy-MM-ddTHHmmsszz"
  • UTC format string "yyyy-MM-ddTHHmmssZ"

In these formats my current local time would be "2009-08-08T151800+03" and UTC "2009-08-08T121800Z"

You can also autodetect the DateTime.Kind with "K" and use "yyyy-MM-ddTHHmmssK", but then you'll have to replace the ':' characters.

Any other suggestions?

Edit: A few notes so far:

local time + time zone format "yyyy-MM-ddTHHmmsszz" is no longer sortable if multiple time zones are involved. In most cases it would make sense to drop the time zone info if it is redundant, and use UTC otherwise.

Another thing is that UTC should always be marked with 'Z', 'GMT' or 'UTC' to prevent guesswork and mistakes.

Julian dates and other stardates are cool because date arithmetic with the gregorian calendar is braindead.

Answer

RichieHindle picture RichieHindle · Aug 8, 2009

I use this:

My-File--2009-12-31--23-59-59.txt
  • No spaces
  • Double dashes to separate the pieces, making each piece easy to see
  • Only one punctuation character (dash) making it easy to type
  • No timezone because for me I'm always working in my local timezone; if I needed one I'd go with UTC and append "--UTC" after the time.