.net 您最喜欢的文件名中的日期和时间格式是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1248747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:07:53  来源:igfitidea点击:

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

.netdatetimefilenames

提问by mika

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.

客观问题是文件名中的时间戳应该是可排序的。但 .NET 可排序日期格式,如“s”( "yyyy-MM-ddTHH:mm:ss") 和“u”( "yyyy-MM-dd HH:mm:ssZ") 在文件名中无效,因为有 ':' 字符。

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

另一件事是,您应该很容易地看到使用的是通用时间还是本地时间。实际上,用户似乎更喜欢当地时间而不是世界时。

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

我大多最终使用具有基本时间格式的ISO 8601

  • Local time format string "yyyy-MM-ddTHHmmsszz"
  • UTC format string "yyyy-MM-ddTHHmmssZ"
  • 本地时间格式字符串 "yyyy-MM-ddTHHmmsszz"
  • UTC 格式字符串 "yyyy-MM-ddTHHmmssZ"

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

在这些格式中,我当前的本地时间是"2009-08-08T151800+03"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.

您还可以使用"K"自动检测 DateTime.Kind并使用"yyyy-MM-ddTHHmmssK",但是您必须替换 ':' 字符。

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.

"yyyy-MM-ddTHHmmsszz"如果涉及多个时区,则本地时间 + 时区格式不再可排序。在大多数情况下,如果时区信息是多余的,则删除时区信息是有意义的,否则使用 UTC。

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

另一件事是 UTC 应始终标有“Z”、“GMT”或“UTC”以防止猜测和错误。

Julian datesand other stardatesare cool because date arithmetic with the gregorian calendaris braindead.

Julian 日期和其他stardates很酷,因为使用公历进行日期算术是脑残。

回答by RichieHindle

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.
  • 没空间了
  • 双破折号将各个部分分开,使每个部分都易于查看
  • 只有一个标点符号(破折号),便于输入
  • 没有时区,因为对我来说,我总是在本地时区工作;如果我需要一个,我会使用 UTC 并--UTC在时间之后附加“ ”。

回答by You

I'd use YYYY-MM-DD HHmmssfor 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.

我将YYYY-MM-DD HHmmss用于文件名,除非特别需要时区或可能需要将它们解析为 ISO 日期;在这些情况下,ISO 日期可能更可取。

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.

编辑:真的不应该需要时区;以 UTC 格式保存所有内容并让人们知道所有内容都是 UTC,这比指定所有内容的时区更有效。

回答by N-ate

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

ILikeDogs_1999-09-23_18-42

回答by Dan Diplo

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.

时间戳是否需要人类可读?如果没有,您可以只使用DateTime.Ticks.ToString()。非常准确,可排序且没有特殊字符。

回答by Carlton Jenke

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

通常我使用 yyyymmdd。如果需要进一步的精度,它会更改为 yyyymmddhhmmss

回答by jinzo

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.

我使用unix时间戳,例如。从纪元过去了多少秒。所有时间均为 UTC。但是,如果您愿意,您可以使用时区数据作为前缀。