C# 可以在文件名或扩展名中使用的 DateTime.ToString() 格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12500091/
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
DateTime.ToString() format that can be used in a filename or extension?
提问by pdizz
I want to add a timestamp to filenames as files are created but most of the DateTime methods I've tried output something with spaces and slashes. For instance:
我想在创建文件时为文件名添加时间戳,但我尝试过的大多数 DateTime 方法都输出带有空格和斜杠的内容。例如:
Debug.WriteLine(DateTime.Now.ToString()); // <-- 9/19/2012 1:41:46 PM
Debug.WriteLine(DateTime.Now.ToShortTimeString()); // <-- 1:41 PM
Debug.WriteLine(DateTime.Now.ToShortDateString()); // <-- 9/19/2012
Debug.WriteLine(DateTime.Now.ToFileTime()); // <-- 129925501061462806
ToFileTime()works but is not exactly human-readable. How can I format the output to a human-readable timestamp with date and time that can be used in a filename or extension? Prefereably something like 2011-19-9--13-45-30?
ToFileTime()工作,但不完全是人类可读的。如何将输出格式化为可在文件名或扩展名中使用的带有日期和时间的人类可读时间戳?最好是这样的2011-19-9--13-45-30?
采纳答案by Kristof Claes
You can use this:
你可以使用这个:
DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss");
回答by Aghilas Yakoub
You can try with
你可以试试
var result = DateTime.Now.ToString("yyyy-MM-d--HH-mm-ss");
回答by Christoffer Lette
回答by Charles Clayton
Personally I like it this way:
我个人喜欢这样:
DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss")
Because it distinguishes between the date and the time.
因为它区分了日期和时间。
回答by Gabe Haack
I have a similar situation but I want a consistent way to be able to use DateTime.Parse from the filename as well, so I went with
我有类似的情况,但我想要一种一致的方式来使用文件名中的 DateTime.Parse,所以我选择了
DateTime.Now.ToString("s").Replace(":", ".") // <-- 2016-10-25T16.50.35
When I want to parse, I can simply reverse the Replace call. This way I don't have to type in any yymmdd stuff or guess what formats DateTime.Parse allows.
当我想解析时,我可以简单地反转 Replace 调用。这样我就不必输入任何 yymmdd 内容或猜测 DateTime.Parse 允许的格式。
回答by rouhollah ghasempour
You can make a path for your file as bellow:
您可以为您的文件创建一个路径,如下所示:
string path = "fileName-"+DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss") + ".txt";
回答by RajeshKdev
The below list of time format specifiers most commonly used.,
以下是最常用的时间格式说明符列表。,
dd-- dayof the month, from 01 through 31.
MM-- month, from 01 through 12.
yyyy-- yearas a four-digit number.
hh-- hour, using a 12-hourclock from 01 to 12.
mm-- minute, from 00 through 59.
ss-- second, from 00 through 59.
HH-- hour, using a 24-hourclock from 00 to 23.
tt-- AM/PMdesignator.
dd-一个月中的第几天,从 01 到 31。
MM-月份,从 01 到 12。
yyyy--四位数的年份。
hh--小时,使用从 01 到 12的12 小时制。
mm-分钟,从 00 到 59。
ss-秒,从 00 到 59。
HH-小时,使用从 00 到 23的24 小时制。
tt--AM/PM指示符。
Using the above you will be able to form a unique name to your file name.
使用上述内容,您将能够为您的文件名形成一个唯一的名称。
Here i have provided example
在这里我提供了示例
string fileName = "fileName_" + DateTime.Now.ToString("MM-dd-yyyy_hh-mm-ss-tt") + ".pdf";
OR
或者
If you don't prefer to use symbols you can try this also.,
如果你不喜欢使用符号,你也可以试试这个。,
string fileName = "fileName_" + DateTime.Now.ToString("MMddyyyyhhmmsstt") + ".pdf";
Hope this helps to someone now or in future. :)
希望这对现在或将来的人有所帮助。:)
回答by ChickenFeet
Using interpolationstring & format specifier:
var filename = $"{DateTime.Now:yyyy.dd.M HH-mm-ss}"
Or if you're not in America:
或者,如果您不在美国:
var filename = $"{DateTime.Now:yyyy.M.dd HH-mm-ss}"
This feature is available in C# 6 and later versions of the language.
此功能在该语言的 C# 6 和更高版本中可用。

