使用在 DateTimeFormatInfo 的属性中存储的标准或自定义模式设置 DateTime 值的格式。
可以通过设置可写 DateTimeFormatInfo 的关联属性用自定义模式替代标准模式。若要确定 DateTimeFormatInfo 是否是可写的,请使用 IsReadOnly 属性。
下表列出了每一种标准模式的标准格式字符以及可以进行设置以修改标准模式的关联 DateTimeFormatInfo 属性。格式字符区分大小写;例如,“g”和“G”所代表的模式稍有不同。
格式字符 | 关联属性/说明 |
d | ShortDatePattern |
D | LongDatePattern |
f | 完整日期和时间(长日期和短时间) |
F | FullDateTimePattern(长日期和长时间) |
g | 常规(短日期和短时间) |
G | 常规(短日期和长时间) |
m、M | MonthDayPattern |
r、R | RFC1123Pattern |
s | 使用当地时间的 SortableDateTimePattern(基于 ISO 8601) |
t | ShortTimePattern |
T | LongTimePattern |
u | UniversalSortableDateTimePattern 用于显示通用时间的格式 |
U | 使用通用时间的完整日期和时间(长日期和长时间) |
y、Y | YearMonthPattern |
下表列出了可被合并以构造自定义模式的模式。这些模式是区分大小写的;例如,识别“MM”,但不识别“mm”。如果自定义模式包含空白字符或用单引号括起来的字符,则输出字符串页也将包含这些字符。未定义为格式模式的一部分或未定义为格式字符的字符按其原义复制。
格式模式 | 说明 |
d、%d | 月中的某一天。一位数的日期没有前导零。如果该格式模式没有与其他格式模式组合,则指定“%d”。 |
dd | 月中的某一天。一位数的日期有一个前导零。 |
ddd | 周中某天的缩写名称,在 AbbreviatedDayNames 中定义。 |
dddd | 周中某天的完整名称,在 DayNames 中定义。 |
M、%M | 月份数字。一位数的月份没有前导零。如果该格式模式没有与其他格式模式组合,则指定“%M”。 |
MM | 月份数字。一位数的月份有一个前导零。 |
MMM | 月份的缩写名称,在 AbbreviatedMonthNames 中定义。 |
MMMM | 月份的完整名称,在 MonthNames 中定义。 |
y、%y | 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示不具有前导零的年份。如果该格式模式没有与其他格式模式组合,则指定“%y”。 |
yy | 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示具有前导零的年份。 |
yyyy | 包括纪元的四位数的年份。 |
gg | 时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。 |
h、%h | 12 小时制的小时。一位数的小时数没有前导零。如果该格式模式没有与其他格式模式组合,则指定“%h”。 |
hh | 12 小时制的小时。一位数的小时数有前导零。 |
H、%H | 24 小时制的小时。一位数的小时数没有前导零。如果该格式模式没有与其他格式模式组合,则指定“%H”。 |
HH | 24 小时制的小时。一位数的小时数有前导零。 |
m、%m | 分钟。一位数的分钟数没有前导零。如果该格式模式没有与其他格式模式组合,则指定“%m”。 |
mm | 分钟。一位数的分钟数有一个前导零。 |
s、%s | 秒。一位数的秒数没有前导零。如果该格式模式没有与其他格式模式组合,则指定“%s”。 |
ss | 秒。一位数的秒数有一个前导零。 |
f、%f | 秒的小数精度为一位。其余数字被截断。如果该格式模式没有与其他格式模式组合,则指定“%f”。 |
ff | 秒的小数精度为两位。其余数字被截断。 |
fff | 秒的小数精度为三位。其余数字被截断。 |
ffff | 秒的小数精度为四位。其余数字被截断。 |
fffff | 秒的小数精度为五位。其余数字被截断。 |
ffffff | 秒的小数精度为六位。其余数字被截断。 |
fffffff | 秒的小数精度为七位。其余数字被截断。 |
F、%F | 显示秒的小数部分的最高有效数字。如果该数字为零,则不显示任何内容。如果该格式模式没有与其他格式模式组合,则指定“%F”。 |
FF | 显示秒的小数部分的两个最高有效数字。但是,不显示尾随的零(两个零数字)。 |
FFF | 显示秒的小数部分的三个最高有效数字。但是,不显示尾随的零(三个零数字)。 |
FFFF | 显示秒的小数部分的四个最高有效数字。但是,不显示尾随的零(四个零数字)。 |
FFFFF | 显示秒的小数部分的五个最高有效数字。但是,不显示尾随的零(五个零数字)。 |
FFFFFF | 显示秒的小数部分的六个最高有效数字。但是,不显示尾随的零(六个零数字)。 |
FFFFFFF | 显示秒的小数部分的七个最高有效数字。但是,不显示尾随的零(七个零数字)。 |
t、%t | 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项的第一个字符(如果存在)。如果该格式模式没有与其他格式模式组合,则指定“%t”。 |
tt | 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项(如果存在)。 |
z、%z | 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数没有前导零。例如,太平洋标准时间是“-8”。如果该格式模式没有与其他格式模式组合,则指定“%z”。 |
zz | 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数有前导零。例如,太平洋标准时间是“-08”。 |
zzz | 完整时区偏移量(“+”或“-”后面跟有小时和分钟)。一位数的小时数和分钟数有前导零。例如,太平洋标准时间是“-08:00”。 |
: | 在 TimeSeparator 中定义的默认时间分隔符。 |
/ | 在 DateSeparator 中定义的默认日期分隔符。 |
% c | 其中 c 是格式模式(如果单独使用)。也就是说,若要单独使用格式模式“d”、“f”、“F”、“h”、“m”、“s”、“t”、“y”、“z”、“H”或“M”,请指定“%d”、“%f”、“%F”、“%h”、“%m”、“%s”、“%t”、“%y”、“%z”、“%H”或“%M”。 如果格式模式与原义字符或其他格式模式合并,则可以省略“%”字符。 |
\ c | 其中 c 是任意字符。照原义显示字符。若要显示反斜杠字符,请使用“\\”。 |
只有上面第二个表中列出的格式模式才能用于创建自定义模式;在第一个表中列出的标准格式字符不能用于创建自定义模式。自定义模式的长度至少为两个字符;例如,
-
DateTime.ToString( "d" ) 返回 DateTime 值;“d”是标准短日期模式。
-
DateTime.ToString( "%d" ) 返回月中的某天;“%d”是自定义模式。
-
DateTime.ToString( "d " ) 返回后面跟有一个空白字符的月中的某天;“d”是自定义模式。
只能为固定区域性或特定区域性创建 DateTimeFormatInfo 或 NumberFormatInfo,而不能为非特定区域性创建它们。有关固定区域性、特定区域性和非特定区域性的更多信息,请参见 CultureInfo 类。
该类实现 ICloneable 接口以启用 DateTimeFormatInfo 对象的复制。它还实现 IFormatProvider 以便为应用程序提供格式化信息。
001 | using System; |
002 | using System.Globalization; |
003 | |
004 | |
005 | public class SamplesDTFI { |
006 | |
007 | public static void Main() { |
008 | |
009 | // Creates and initializes a DateTimeFormatInfo associated with the en-US culture. |
010 | DateTimeFormatInfo myDTFI = new CultureInfo( "en-US" , false ).DateTimeFormat; |
011 | |
012 | // Creates a DateTime with the Gregorian date January 3, 2002 (year=2002, month=1, day=3). |
013 | // The Gregorian calendar is the default calendar for the en-US culture. |
014 | DateTime myDT = new DateTime( 2002, 1, 3 ); |
015 | |
016 | // Displays the format pattern associated with each format character. |
017 | Console.WriteLine( "FORMAT en-US EXAMPLE" ); |
018 | Console.WriteLine( "CHAR VALUE OF ASSOCIATED PROPERTY, IF ANY\n" ); |
019 | Console.WriteLine( " d {0}" , myDT.ToString( "d" ) ); |
020 | Console.WriteLine( " {0} {1}\n" , myDTFI.ShortDatePattern, "(ShortDatePattern)" ); |
021 | Console.WriteLine( " D {0}" , myDT.ToString( "D" ) ); |
022 | Console.WriteLine( " {0} {1}\n" , myDTFI.LongDatePattern, "(LongDatePattern)" ); |
023 | Console.WriteLine( " f {0}\n" , myDT.ToString( "f" ) ); |
024 | Console.WriteLine( " F {0}" , myDT.ToString( "F" ) ); |
025 | Console.WriteLine( " {0} {1}\n" , myDTFI.FullDateTimePattern, "(FullDateTimePattern)" ); |
026 | Console.WriteLine( " g {0}\n" , myDT.ToString( "g" ) ); |
027 | Console.WriteLine( " G {0}\n" , myDT.ToString( "G" ) ); |
028 | Console.WriteLine( " m {0}" , myDT.ToString( "m" ) ); |
029 | Console.WriteLine( " {0} {1}\n" , myDTFI.MonthDayPattern, "(MonthDayPattern)" ); |
030 | Console.WriteLine( " M {0}" , myDT.ToString( "M" ) ); |
031 | Console.WriteLine( " {0} {1}\n" , myDTFI.MonthDayPattern, "(MonthDayPattern)" ); |
032 | Console.WriteLine( " r {0}" , myDT.ToString( "r" ) ); |
033 | Console.WriteLine( " {0} {1}\n" , myDTFI.RFC1123Pattern, "(RFC1123Pattern)" ); |
034 | Console.WriteLine( " R {0}" , myDT.ToString( "R" ) ); |
035 | Console.WriteLine( " {0} {1}\n" , myDTFI.RFC1123Pattern, "(RFC1123Pattern)" ); |
036 | Console.WriteLine( " s {0}" , myDT.ToString( "s" ) ); |
037 | Console.WriteLine( " {0} {1}\n" , myDTFI.SortableDateTimePattern, "(SortableDateTimePattern)" ); |
038 | Console.WriteLine( " t {0}" , myDT.ToString( "t" ) ); |
039 | Console.WriteLine( " {0} {1}\n" , myDTFI.ShortTimePattern, "(ShortTimePattern)" ); |
040 | Console.WriteLine( " T {0}" , myDT.ToString( "T" ) ); |
041 | Console.WriteLine( " {0} {1}\n" , myDTFI.LongTimePattern, "(LongTimePattern)" ); |
042 | Console.WriteLine( " u {0}" , myDT.ToString( "u" ) ); |
043 | Console.WriteLine( " {0} {1}\n" , myDTFI.UniversalSortableDateTimePattern, "(UniversalSortableDateTimePattern)" ); |
044 | Console.WriteLine( " U {0}\n" , myDT.ToString( "U" ) ); |
045 | Console.WriteLine( " y {0}" , myDT.ToString( "y" ) ); |
046 | Console.WriteLine( " {0} {1}\n" , myDTFI.YearMonthPattern, "(YearMonthPattern)" ); |
047 | Console.WriteLine( " Y {0}" , myDT.ToString( "Y" ) ); |
048 | Console.WriteLine( " {0} {1}\n" , myDTFI.YearMonthPattern, "(YearMonthPattern)" ); |
049 | |
050 | } |
051 | |
052 | } |
053 | |
054 | /* |
055 | This code produces the following output. |
056 | |
057 | FORMAT en-US EXAMPLE |
058 | CHAR VALUE OF ASSOCIATED PROPERTY, IF ANY |
059 | |
060 | d 1/3/2002 |
061 | M/d/yyyy (ShortDatePattern) |
062 | |
063 | D Thursday, January 03, 2002 |
064 | dddd, MMMM dd, yyyy (LongDatePattern) |
065 | |
066 | f Thursday, January 03, 2002 12:00 AM |
067 | |
068 | F Thursday, January 03, 2002 12:00:00 AM |
069 | dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern) |
070 | |
071 | g 1/3/2002 12:00 AM |
072 | |
073 | G 1/3/2002 12:00:00 AM |
074 | |
075 | m January 03 |
076 | MMMM dd (MonthDayPattern) |
077 | |
078 | M January 03 |
079 | MMMM dd (MonthDayPattern) |
080 | |
081 | r Thu, 03 Jan 2002 00:00:00 GMT |
082 | ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern) |
083 | |
084 | R Thu, 03 Jan 2002 00:00:00 GMT |
085 | ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern) |
086 | |
087 | s 2002-01-03T00:00:00 |
088 | yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern) |
089 | |
090 | t 12:00 AM |
091 | h:mm tt (ShortTimePattern) |
092 | |
093 | T 12:00:00 AM |
094 | h:mm:ss tt (LongTimePattern) |
095 | |
096 | u 2002-01-03 00:00:00Z |
097 | yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern) |
098 | |
099 | U Thursday, January 03, 2002 8:00:00 AM |
100 | |
101 | y January, 2002 |
102 | MMMM, yyyy (YearMonthPattern) |
103 | |
104 | Y January, 2002 |
105 | MMMM, yyyy (YearMonthPattern) |
106 | |
107 | */ |