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

.NetDatetimeFilenames

.Net Problem Overview


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.

.Net Solutions


Solution 1 - .Net

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.

Solution 2 - .Net

I'd use YYYY-MM-DD HHmmss for filenames, unless there is a particular need for timezones or a possible need to parse them into ISO dates; in those cases an ISO date would probably be preferrable.

Edit: Timezones shouldn't really ever be required; saving everything in UTC and letting people know that it's all UTC is more efficient than specifying the time zone of everything.

Solution 3 - .Net

Here is what I use:

private static string CreateMeaningfulFileName(string friendlyName, DateTime date)
{
    StringBuilder sb = new StringBuilder();
    foreach (string s in friendlyName.Split(new char[] { ' ' }))//remove spaces
    {
        sb.Append(CultureInfo.CurrentCulture.TextInfo.ToTitleCase(s.ToLower()));//capitalize each segment
    }
    sb.Append("_" + date.ToString("yyyy-MM-dd_HH-mm"));//add date
    return sb.ToString();
}

It takes a date and description. Let's use "I like DOGS". Results in:

ILikeDogs_1999-09-23_18-42

Solution 4 - .Net

Is there a requirement for the timestamp to be human readable? If not, you could just use DateTime.Ticks.ToString(). Very accurate, sortable and no special characters.

Solution 5 - .Net

Normally I use yyyymmdd. If further precision is needed it changes to yyyymmddhhmmss

Solution 6 - .Net

I use unix timestamps, eg. how many seconds passed from epoch. All times in UTC. But guess you could prefix w/ timezone data if you wish.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmikaView Question on Stackoverflow
Solution 1 - .NetRichieHindleView Answer on Stackoverflow
Solution 2 - .NetYouView Answer on Stackoverflow
Solution 3 - .NetN-ateView Answer on Stackoverflow
Solution 4 - .NetDan DiploView Answer on Stackoverflow
Solution 5 - .NetCarlton JenkeView Answer on Stackoverflow
Solution 6 - .NetjinzoView Answer on Stackoverflow