[记录]标准 DateTime 格式字符串
格式说明符 | 名称 | 说明 |
---|---|---|
d | 短日期模式 | 显示由与当前线程关联的 DateTimeFormatInfo.ShortDatePattern 属性定义的模式或者由指定格式提供程序定义的模式。 |
D | 长日期模式 | 显示由与当前线程关联的 DateTimeFormatInfo.LongDatePattern 属性定义的模式或者由指定格式提供程序定义的模式。 |
t | 短时间模式 | 显示由与当前线程关联的 DateTimeFormatInfo.ShortTimePattern 属性定义的模式或者由指定格式提供程序定义的模式。 |
T | 长时间模式 | 显示由与当前线程关联的 DateTimeFormatInfo.LongTimePattern 属性定义的模式或者由指定格式提供程序定义的模式。 |
f | 完整日期/时间模式(短时间) | 显示长日期和短时间模式的组合,由空格分隔。 |
F | 完整日期/时间模式(长时间) | 显示由与当前线程关联的 DateTimeFormatInfo.FullDateTimePattern 属性定义的模式或者由指定格式提供程序定义的模式。 |
g | 常规日期/时间模式(短时间) | 显示短日期和短时间模式的组合,由空格分隔。 |
G | 常规日期/时间模式(长时间) | 显示短日期和长时间模式的组合,由空格分隔。 |
M 或 m | 月日模式 | 显示由与当前线程关联的 DateTimeFormatInfo.MonthDayPattern 属性定义的模式或者由指定格式提供程序定义的模式。 |
R 或 r | RFC1123 模式 | 显示由与当前线程关联的 DateTimeFormatInfo.RFC1123Pattern 属性定义的模式或者由指定格式提供程序定义的模式。这是定义的标准,并且属性是只读的;因此,无论所使用的区域性或所提供的格式提供程序是什么,它总是相同的。属性引用 CultureInfo.InvariantCulture 属性并遵照自定义模式“ddd, dd MMM yyyy HH:mm:ss G\MT”。请注意,“GMT”中的“M”需要转义符,因此它不被解释。格式化并不修改 DateTime 的值,所以您必须在格式化之前将值调整为 GMT。 |
s | 可排序的日期/时间模式;符合 ISO 8601 | 显示由与当前线程关联的 DateTimeFormatInfo.SortableDateTimePattern 属性定义的模式或者由指定格式提供程序定义的模式。属性引用 CultureInfo.InvariantCulture 属性,格式遵照自定义模式“yyyy-MM-ddTHH:mm:ss”。 |
u | 通用的可排序日期/时间模式 | 显示由与当前线程关联的 DateTimeFormatInfo.UniversalSortableDateTimePattern 属性定义的模式或者由指定格式提供程序定义的模式。因为它是定义的标准,并且属性是只读的,因此无论区域性或格式提供程序是什么,模式总是相同的。格式化遵照自定义模式“yyyy-MM-dd HH:mm:ssZ”。格式化日期和时间时不进行时区转换;所以,请在使用格式说明符之前将本地日期和时间转换为通用时间。 |
U | 通用的可排序日期/时间模式 | 显示由与当前线程关联的 DateTimeFormatInfo.FullDateTimePattern 属性定义的模式或者由指定格式提供程序定义的模式。请注意,显示的时间是通用时间,而不是本地时间。 |
Y 或 y | 年月模式 | 显示由与当前线程关联的 DateTimeFormatInfo.YearMonthPattern 属性定义的模式或者由指定格式提供程序定义的模式。 |
任何其他单个字符 | 未知说明符 |
DateTime dt = DateTime.Now;
DateTimeFormatInfo dfi = new DateTimeFormatInfo();
CultureInfo ci = new CultureInfo("de-DE");
// Make up a new custom DateTime pattern, for demonstration.
dfi.MonthDayPattern = "MM-MMMM, ddd-dddd";
// Use the DateTimeFormat from the culture associated
// with the current thread.
Console.WriteLine( dt.ToString("d") );
Console.WriteLine( dt.ToString("m") );
// Use the DateTimeFormat from the specific culture passed.
Console.WriteLine( dt.ToString("d", ci ) );
// Use the settings from the DateTimeFormatInfo object passed.
Console.WriteLine( dt.ToString("m", dfi ) );
// Reset the current thread to a different culture.
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-BE");
Console.WriteLine( dt.ToString("d") );
DateTimeFormatInfo dfi = new DateTimeFormatInfo();
CultureInfo ci = new CultureInfo("de-DE");
// Make up a new custom DateTime pattern, for demonstration.
dfi.MonthDayPattern = "MM-MMMM, ddd-dddd";
// Use the DateTimeFormat from the culture associated
// with the current thread.
Console.WriteLine( dt.ToString("d") );
Console.WriteLine( dt.ToString("m") );
// Use the DateTimeFormat from the specific culture passed.
Console.WriteLine( dt.ToString("d", ci ) );
// Use the settings from the DateTimeFormatInfo object passed.
Console.WriteLine( dt.ToString("m", dfi ) );
// Reset the current thread to a different culture.
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-BE");
Console.WriteLine( dt.ToString("d") );
http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpguide/html/cpconstandarddatetimeformatstrings.asp