我在处理我正在开发的组件的日期时间格式时遇到以下问题。
string value = DateTime.Now.ToString( ); // value = "8/3/2010 2:20:49 PM"
如何找出 value
引用的日期格式 --> "M/d/yyyy h:mm:ss tt"
我希望在导出过程中存储当前的日期时间格式,并在导入过程中使用相同的格式。 DateTimeFormatInfo.CurrentInfo
和 CultureInfo.CurrentCulture.DateTimeFormat
中提供的各种 api 不提供此信息。
我知道的一个解决方案是使用 dt.ToString( "u") 以通用格式存储和解析日期时间,但我很好奇如何获得上述格式。
最佳答案
根据文档:
DateTime.ToString()
The value of this instance is formatted using the general format specifier, 'G', as described in the Formatting Overview topic. The return value is identical to the value returned by
ToString ("G", null)
.
按照文档链接格式概述 -> 日期和时间格式字符串 -> 标准日期时间格式字符串,我们可以了解
G
General date/time pattern (long time)
Displays a combination of the short date and long time patterns, separated by a space.
因此,您想要的格式应该可以通过组合 ShortDatePattern
来获得和LongTimePattern
Thread.CurrentThread.CurrentCulture.DateTimeFormat
的成员.
关于c# - 找出调用 DateTime.ToString( ) 时获得的 DateTimeFormat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3394460/