可以在文件名或扩展名中使用的DateTime.ToString()格式? [英] DateTime.ToString() format that can be used in a filename or extension?
本文介绍了可以在文件名或扩展名中使用的DateTime.ToString()格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在创建文件时为文件名添加时间戳,但是我尝试使用的大多数DateTime方法输出带有空格和斜杠的内容。例如:
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:
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()
有效,但并非完全可读。如何将输出格式化为可以在文件名或扩展名中使用的日期和时间,以便于人类理解?最好是类似 2011-19-9--13-45-30
?
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
?
推荐答案
您可以使用:
DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss");
这篇关于可以在文件名或扩展名中使用的DateTime.ToString()格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文