96

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.

mika
  • 6,404
  • 2
  • 33
  • 38
  • 2
    I agree with all of this except that I *do* find it important in the big scheme of things. Your format is the best -I would even use 4 digit timezone as specified in ISO8601, to accommodate non-integer zones. It has always bothered me that ISO8601 does not address the forbidden colon in filenames. – hpekristiansen Jan 30 '16 at 22:23
  • I propose a format based on ISO 8601 in a post at http://blog.xam.de/2016/07/standard-format-for-time-stamps-in-file.html -- would make our world easier, if we could agree on a format :-) – Dr. Max Völkel Jul 19 '16 at 08:07
  • "closed as not constructive"?? There are reasons for and against every proposal, so it **is** constructive. No need to shy away from a question about the finer philosophical details of file naming :D – tanius Apr 08 '19 at 08:55
  • I think its a great question. – Jacob Lee May 27 '21 at 15:17

6 Answers6

62

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.
RichieHindle
  • 244,085
  • 44
  • 340
  • 385
  • 6
    I do approximately the same although I *start* with the timestamp and then the 'my-file' stuff since that allows one to view the files in chronological order by just ordering the filenames alphabetically. – ChristopheD Aug 08 '09 at 13:08
  • 2
    @ChristopheD: Sure - I'd do the same in that case. For my answer, I was thinking of the case where you have several versions of several different files, and you want them grouped by filename. – RichieHindle Aug 08 '09 at 13:38
  • 4
    Personally I prefer to use underscore (`_`) instead of double dash to separate the pieces. (e.g. `My-File_2009-12-31_23-59-59.txt`) – Elie G. May 28 '18 at 14:00
  • A `name_-_2019-11-04--15-04-36.642621403_-_2019-11-04--15-04-36.642622803.ext` is ok for Linux shell. No time zone, but with nanoseconds precision. – Ivan Black Nov 04 '19 at 11:39
16

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.

You
  • 20,363
  • 3
  • 45
  • 63
  • 11
    ouch, spaces in your filenames? – grateful.dev Aug 08 '09 at 12:31
  • 10
    What's wrong with spaces? – Joey Aug 08 '09 at 12:33
  • 47
    Yes, spaces are heinous. I will personally track down people who use spaces in filenames and beat them to death with a wet stick of celery :-) – paxdiablo Aug 08 '09 at 12:33
  • 1
    @Saggi; well I probably deserved that one, but seriously -- show me one system that doesn't handle filenames with spaces? From what I could gather, there were no URIs involved and thus I don't worry too much about spaces. You could of course exchange that space for something else, such as an underscore or `T`, but that's less natural when you read it. – You Aug 08 '09 at 12:33
  • 1
    I'll vote for this one (but I'd never use spaces), the reason being the 4-digit year which will put off the y2k1 problem (but not the y10k one, but who really gives a XXXX about that?). – paxdiablo Aug 08 '09 at 12:38
  • I tend to use yyyy-mm-dd-hh-mm-ss. – paxdiablo Aug 08 '09 at 12:38
  • That one makes it kinda hard to see where the date ends and the time starts, though. But I don't have a nice format which solves that, too :/ – Joey Aug 08 '09 at 13:16
  • 1
    @Pax: back in 1970 they said the same for 2000! ;) – Stefano Borini Aug 08 '09 at 14:30
  • 1
    @You it's not the filesystems that run into trouble with spaces but rather scripts and poorly designed programs that don't deal with them correctly. – Nathan Osman Jan 05 '15 at 19:36
  • @NathanOsman: Well, yes. But if the software/script you're using doesn't handle spaces it is as you say poorly designed and should be fixed or replaced. – You Jan 05 '15 at 20:48
  • Late to the game, but the reason I dislike spaces (and colons) in names is you can't double-click to select the filename (at least in the Linux shell/putty I'm using) from the command line. – Robert Lugg Jun 27 '18 at 16:11
  • I use this format as well, but with `_T_` as separator instead of the space. It is similar to the `T` used in ISO 8601 but more readable. Plus, `YYYY-MM-DDThhmmss` would not be ISO 8601 compliant as it uses mixed extended and basic representations. Using a space (like you) or `_` or `_T_` it is technically compliant (but stretching it) as it's a separate representation for date and time. – tanius Apr 08 '19 at 08:51
11

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

N-ate
  • 3,202
  • 26
  • 37
3

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.

Dan Diplo
  • 24,377
  • 4
  • 61
  • 86
  • 2
    I tend to try and make everything human readable, if at all possible. This is one of the great values of schemes like JSON notation. Even if you don't think it needs to be human readable, you never know if you'll want it to be human readable later. Also, it tends to make for easier debugging. – Edan Maor Jun 26 '10 at 19:40
  • The link is down, what'd it go to? – rbatt Aug 02 '15 at 21:35
  • @rbatt I think it was just a temporary MSDN issue - should be back now. – Dan Diplo Aug 04 '15 at 11:12
2

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

Carlton Jenke
  • 2,897
  • 3
  • 24
  • 29
1

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.

jinzo
  • 29
  • 3