[英]DateTime.ToString() format that can be used in a filename or extension?
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.我想在创建文件时为文件名添加时间戳,但我尝试过的大多数 DateTime 方法都输出带有空格和斜杠的内容。 For instance:例如:
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. ToFileTime()
有效但不完全是人类可读的。 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
?最好是2011-19-9--13-45-30
?
你可以使用这个:
DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss");
我会使用ISO 8601格式,没有分隔符:
DateTime.Now.ToString("yyyyMMddTHHmmss")
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.当我想解析时,我可以简单地反转 Replace 调用。 This way I don't have to type in any yymmdd stuff or guess what formats DateTime.Parse allows.这样我就不必输入任何 yymmdd 内容或猜测 DateTime.Parse 允许的格式。
The below list of time format specifiers most commonly used.,以下是最常用的时间格式说明符列表。,
dd -- day of the month, from 01 through 31. dd - 一个月中的第几天,从 01 到 31。
MM -- month , from 01 through 12. MM -月份,从 01 到 12。
yyyy -- year as a four-digit number. yyyy -- 四位数的年份。
hh -- hour , using a 12-hour clock from 01 to 12. hh --小时,使用从 01 到 12 的12 小时制。
mm -- minute , from 00 through 59. mm -分钟,从 00 到 59。
ss -- second , from 00 through 59. ss -秒,从 00 到 59。
HH -- hour , using a 24-hour clock from 00 to 23. HH -小时,使用从 00 到 23 的24 小时制。
tt -- AM/PM designator. 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.希望这对现在或将来的人有所帮助。 :) :)
Personally I like it this way:我个人喜欢这样:
DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss")
Because it distinguishes between the date and the time.因为它区分了日期和时间。
你可以试试
var result = DateTime.Now.ToString("yyyy-MM-d--HH-mm-ss");
Using interpolation string & format specifier :使用插值字符串和 格式说明符:
var filename = $"{DateTime.Now:yyyy.dd.M HH-mm-ss}"
Example Output for January 1st, 2020 at 10:40:45AM: 2020 年 1 月 1 日上午 10:40:45 的示例输出:
2020.28.01 10-40-45 2020.28.01 10-40-45
Or if you want a standard date format:或者,如果您想要标准日期格式:
var filename = $"{DateTime.Now:yyyy.M.dd HH-mm-ss}"
2020.01.28 10-40-45 2020.01.28 10-40-45
Note: this feature is available in C# 6 and later versions of the language.注意:此功能在 C# 6 及更高版本的语言中可用。
您可以为您的文件创建一个路径,如下所示:
string path = "fileName-"+DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss") + ".txt";
Try this: 试试这个:
time.ToLongDateString()
Output: "Friday, August 24, 2018" 输出:“2018年8月24日星期五”
or use formatting: 或使用格式:
time.ToString("yyyy_MM_dd")
Output: "2018_08_24" 输出:“2018_08_24”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.