DateTimeFormatInfo 类

定义如何根据区域性设置 DateTime 值的格式并显示这些值。

**命名空间:**System.Globalization
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class DateTimeFormatInfo
    Implements ICloneable, IFormatProvider
用法
Dim instance As DateTimeFormatInfo
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public sealed class DateTimeFormatInfo : ICloneable, IFormatProvider
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class DateTimeFormatInfo sealed : ICloneable, IFormatProvider
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class DateTimeFormatInfo implements ICloneable, IFormatProvider
SerializableAttribute 
ComVisibleAttribute(true) 
public final class DateTimeFormatInfo implements ICloneable, IFormatProvider

备注

此类包含各种信息,例如日期模式、时间模式和 AM/PM 指示项。

若要为特定区域性创建 DateTimeFormatInfo,请为该区域性创建 CultureInfo 并检索 CultureInfo.DateTimeFormat 属性。若要为当前线程的区域性创建 DateTimeFormatInfo,请使用 CurrentInfo 属性。若要为固定区域性创建 DateTimeFormatInfo,请为只读版本使用 InvariantInfo 属性,或为可写版本使用 DateTimeFormatInfo 构造函数。不可能为非特定区域性创建 DateTimeFormatInfo

用户可以通过“控制面板”中的“区域和语言选项”(或“区域选项”或“区域设置”)选择重写某些与 Windows 的当前区域性关联的值。例如,用户可能选择以另一种格式显示日期,或选择使用区域性默认设置以外的货币。如果 CultureInfo.UseUserOverride 属性设置为 true,则还将从用户设置中检索 CultureInfo.DateTimeFormat 实例、CultureInfo.NumberFormat 实例和 CultureInfo.TextInfo 实例的属性。如果用户设置与 CultureInfo 的关联区域性不兼容(例如选定的日历不属于 OptionalCalendars 其中之一),则方法结果和属性值是未定义的。

使用在 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

AMDesignatorPMDesignator 中定义的 AM/PM 指示项的第一个字符(如果存在)。如果该格式模式没有与其他格式模式组合,则指定“%t”。

tt

AMDesignatorPMDesignator 中定义的 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”是自定义模式。

只能为固定区域性或特定区域性创建 DateTimeFormatInfoNumberFormatInfo,而不能为非特定区域性创建它们。有关固定区域性、特定区域性和非特定区域性的更多信息,请参见 CultureInfo 类。

该类实现 ICloneable 接口以启用 DateTimeFormatInfo 对象的复制。它还实现 IFormatProvider 以便为应用程序提供格式化信息。

示例

下面的代码示例使用格式字符输出 en-US 区域性的不同格式模式。该示例还将显示与格式字符关联的属性值。

Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic

Public Class SamplesDTFI

   Public Shared Sub Main()

      ' Creates and initializes a DateTimeFormatInfo associated with the en-US culture.
      Dim myDTFI As DateTimeFormatInfo = New CultureInfo("en-US", False).DateTimeFormat
      
      ' Creates a DateTime with the Gregorian date January 3, 2002 (year=2002, month=1, day=3).
      ' The Gregorian calendar is the default calendar for the en-US culture.
      Dim myDT As New DateTime(2002, 1, 3)
      
      ' Displays the format pattern associated with each format character.
      Console.WriteLine("FORMAT  en-US EXAMPLE")
      Console.WriteLine("CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY")
      Console.WriteLine()
      Console.WriteLine("  d     {0}", myDT.ToString("d"))
      Console.WriteLine("        {0} {1}", myDTFI.ShortDatePattern, "(ShortDatePattern)")
      Console.WriteLine()
      Console.WriteLine("  D     {0}", myDT.ToString("D"))
      Console.WriteLine("        {0} {1}", myDTFI.LongDatePattern, "(LongDatePattern)")
      Console.WriteLine()
      Console.WriteLine("  f     {0}", myDT.ToString("f"))
      Console.WriteLine()
      Console.WriteLine("  F     {0}", myDT.ToString("F"))
      Console.WriteLine("        {0} {1}", myDTFI.FullDateTimePattern, "(FullDateTimePattern)")
      Console.WriteLine()
      Console.WriteLine("  g     {0}", myDT.ToString("g"))
      Console.WriteLine()
      Console.WriteLine("  G     {0}", myDT.ToString("G"))
      Console.WriteLine()
      Console.WriteLine("  m     {0}", myDT.ToString("m"))
      Console.WriteLine("        {0} {1}", myDTFI.MonthDayPattern, "(MonthDayPattern)")
      Console.WriteLine()
      Console.WriteLine("  M     {0}", myDT.ToString("M"))
      Console.WriteLine("        {0} {1}", myDTFI.MonthDayPattern, "(MonthDayPattern)")
      Console.WriteLine()
      Console.WriteLine("  r     {0}", myDT.ToString("r"))
      Console.WriteLine("        {0} {1}", myDTFI.RFC1123Pattern, "(RFC1123Pattern)")
      Console.WriteLine()
      Console.WriteLine("  R     {0}", myDT.ToString("R"))
      Console.WriteLine("        {0} {1}", myDTFI.RFC1123Pattern, "(RFC1123Pattern)")
      Console.WriteLine()
      Console.WriteLine("  s     {0}", myDT.ToString("s"))
      Console.WriteLine("        {0} {1}", myDTFI.SortableDateTimePattern, "(SortableDateTimePattern)")
      Console.WriteLine()
      Console.WriteLine("  t     {0}", myDT.ToString("t"))
      Console.WriteLine("        {0} {1}", myDTFI.ShortTimePattern, "(ShortTimePattern)")
      Console.WriteLine()
      Console.WriteLine("  T     {0}", myDT.ToString("T"))
      Console.WriteLine("        {0} {1}", myDTFI.LongTimePattern, "(LongTimePattern)")
      Console.WriteLine()
      Console.WriteLine("  u     {0}", myDT.ToString("u"))
      Console.WriteLine("        {0} {1}", myDTFI.UniversalSortableDateTimePattern, "(UniversalSortableDateTimePattern)")
      Console.WriteLine()
      Console.WriteLine("  U     {0}", myDT.ToString("U"))
      Console.WriteLine()
      Console.WriteLine("  y     {0}", myDT.ToString("y"))
      Console.WriteLine("        {0} {1}", myDTFI.YearMonthPattern, "(YearMonthPattern)")
      Console.WriteLine()
      Console.WriteLine("  Y     {0}", myDT.ToString("Y"))
      Console.WriteLine("        {0} {1}", myDTFI.YearMonthPattern, "(YearMonthPattern)")

   End Sub 'Main 

End Class 'SamplesDTFI


'This code produces the following output.
'
'FORMAT  en-US EXAMPLE
'CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY
'
'  d     1/3/2002
'        M/d/yyyy (ShortDatePattern)
'
'  D     Thursday, January 03, 2002
'        dddd, MMMM dd, yyyy (LongDatePattern)
'
'  f     Thursday, January 03, 2002 12:00 AM
'
'  F     Thursday, January 03, 2002 12:00:00 AM
'        dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)
'
'  g     1/3/2002 12:00 AM
'
'  G     1/3/2002 12:00:00 AM
'
'  m     January 03
'        MMMM dd (MonthDayPattern)
'
'  M     January 03
'        MMMM dd (MonthDayPattern)
'
'  r     Thu, 03 Jan 2002 00:00:00 GMT
'        ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
'
'  R     Thu, 03 Jan 2002 00:00:00 GMT
'        ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
'
'  s     2002-01-03T00:00:00
'        yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)
'
'  t     12:00 AM
'        h:mm tt (ShortTimePattern)
'
'  T     12:00:00 AM
'        h:mm:ss tt (LongTimePattern)
'
'  u     2002-01-03 00:00:00Z
'        yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)
'
'  U     Thursday, January 03, 2002 8:00:00 AM
'
'  y     January, 2002
'        MMMM, yyyy (YearMonthPattern)
'
'  Y     January, 2002
'        MMMM, yyyy (YearMonthPattern)
'
using System;
using System.Globalization;


public class SamplesDTFI  {

   public static void Main()  {

      // Creates and initializes a DateTimeFormatInfo associated with the en-US culture.
      DateTimeFormatInfo myDTFI = new CultureInfo( "en-US", false ).DateTimeFormat;

      // Creates a DateTime with the Gregorian date January 3, 2002 (year=2002, month=1, day=3).
      // The Gregorian calendar is the default calendar for the en-US culture.
      DateTime myDT = new DateTime( 2002, 1, 3 );

      // Displays the format pattern associated with each format character.
      Console.WriteLine( "FORMAT  en-US EXAMPLE" );
      Console.WriteLine( "CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY\n" );
      Console.WriteLine( "  d     {0}", myDT.ToString("d") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.ShortDatePattern, "(ShortDatePattern)" );
      Console.WriteLine( "  D     {0}", myDT.ToString("D") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.LongDatePattern, "(LongDatePattern)" );
      Console.WriteLine( "  f     {0}\n", myDT.ToString("f") );
      Console.WriteLine( "  F     {0}", myDT.ToString("F") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.FullDateTimePattern, "(FullDateTimePattern)" );
      Console.WriteLine( "  g     {0}\n", myDT.ToString("g") );
      Console.WriteLine( "  G     {0}\n", myDT.ToString("G") );
      Console.WriteLine( "  m     {0}", myDT.ToString("m") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.MonthDayPattern, "(MonthDayPattern)" );
      Console.WriteLine( "  M     {0}", myDT.ToString("M") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.MonthDayPattern, "(MonthDayPattern)" );
      Console.WriteLine( "  r     {0}", myDT.ToString("r") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.RFC1123Pattern, "(RFC1123Pattern)" );
      Console.WriteLine( "  R     {0}", myDT.ToString("R") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.RFC1123Pattern, "(RFC1123Pattern)" );
      Console.WriteLine( "  s     {0}", myDT.ToString("s") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.SortableDateTimePattern, "(SortableDateTimePattern)" );
      Console.WriteLine( "  t     {0}", myDT.ToString("t") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.ShortTimePattern, "(ShortTimePattern)" );
      Console.WriteLine( "  T     {0}", myDT.ToString("T") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.LongTimePattern, "(LongTimePattern)" );
      Console.WriteLine( "  u     {0}", myDT.ToString("u") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.UniversalSortableDateTimePattern, "(UniversalSortableDateTimePattern)" );
      Console.WriteLine( "  U     {0}\n", myDT.ToString("U") );
      Console.WriteLine( "  y     {0}", myDT.ToString("y") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.YearMonthPattern, "(YearMonthPattern)" );
      Console.WriteLine( "  Y     {0}", myDT.ToString("Y") );
      Console.WriteLine( "        {0} {1}\n", myDTFI.YearMonthPattern, "(YearMonthPattern)" );

   }

}

/*
This code produces the following output.

FORMAT  en-US EXAMPLE
CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY

  d     1/3/2002
        M/d/yyyy (ShortDatePattern)

  D     Thursday, January 03, 2002
        dddd, MMMM dd, yyyy (LongDatePattern)

  f     Thursday, January 03, 2002 12:00 AM

  F     Thursday, January 03, 2002 12:00:00 AM
        dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)

  g     1/3/2002 12:00 AM

  G     1/3/2002 12:00:00 AM

  m     January 03
        MMMM dd (MonthDayPattern)

  M     January 03
        MMMM dd (MonthDayPattern)

  r     Thu, 03 Jan 2002 00:00:00 GMT
        ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)

  R     Thu, 03 Jan 2002 00:00:00 GMT
        ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)

  s     2002-01-03T00:00:00
        yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)

  t     12:00 AM
        h:mm tt (ShortTimePattern)

  T     12:00:00 AM
        h:mm:ss tt (LongTimePattern)

  u     2002-01-03 00:00:00Z
        yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)

  U     Thursday, January 03, 2002 8:00:00 AM

  y     January, 2002
        MMMM, yyyy (YearMonthPattern)

  Y     January, 2002
        MMMM, yyyy (YearMonthPattern)

*/
using namespace System;
using namespace System::Globalization;
int main()
{
   
   // Creates and initializes a DateTimeFormatInfo associated with the en-US culture.
   CultureInfo^ MyCI = gcnew CultureInfo( "en-US",false );
   DateTimeFormatInfo^ myDTFI = MyCI->DateTimeFormat;
   
   // Creates a DateTime with the Gregorian date January 3, 2002 (year=2002, month=1, day=3).
   // The Gregorian calendar is the default calendar for the en-US culture.
   DateTime myDT = DateTime(2002,1,3);
   
   // Displays the format pattern associated with each format character.
   Console::WriteLine( "FORMAT  en-US EXAMPLE" );
   Console::WriteLine( "CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY\n" );
   Console::WriteLine( "  d {0}", myDT.ToString( "d" ) );
   Console::WriteLine( " {0} {1}\n", myDTFI->ShortDatePattern, "(ShortDatePattern)" );
   Console::WriteLine( "  D {0}", myDT.ToString( "D" ) );
   Console::WriteLine( " {0} {1}\n", myDTFI->LongDatePattern, "(LongDatePattern)" );
   Console::WriteLine( "  f {0}\n", myDT.ToString( "f" ) );
   Console::WriteLine( "  F {0}", myDT.ToString( "F" ) );
   Console::WriteLine( " {0} {1}\n", myDTFI->FullDateTimePattern, "(FullDateTimePattern)" );
   Console::WriteLine( "  g {0}\n", myDT.ToString( "g" ) );
   Console::WriteLine( "  G {0}\n", myDT.ToString( "G" ) );
   Console::WriteLine( "  m {0}", myDT.ToString( "m" ) );
   Console::WriteLine( " {0} {1}\n", myDTFI->MonthDayPattern, "(MonthDayPattern)" );
   Console::WriteLine( "  M {0}", myDT.ToString( "M" ) );
   Console::WriteLine( " {0} {1}\n", myDTFI->MonthDayPattern, "(MonthDayPattern)" );
   Console::WriteLine( "  r {0}", myDT.ToString( "r" ) );
   Console::WriteLine( " {0} {1}\n", myDTFI->RFC1123Pattern, "(RFC1123Pattern)" );
   Console::WriteLine( "  R {0}", myDT.ToString( "R" ) );
   Console::WriteLine( " {0} {1}\n", myDTFI->RFC1123Pattern, "(RFC1123Pattern)" );
   Console::WriteLine( "  s {0}", myDT.ToString( "s" ) );
   Console::WriteLine( " {0} {1}\n", myDTFI->SortableDateTimePattern, "(SortableDateTimePattern)" );
   Console::WriteLine( "  t {0}", myDT.ToString( "t" ) );
   Console::WriteLine( " {0} {1}\n", myDTFI->ShortTimePattern, "(ShortTimePattern)" );
   Console::WriteLine( "  T {0}", myDT.ToString( "T" ) );
   Console::WriteLine( " {0} {1}\n", myDTFI->LongTimePattern, "(LongTimePattern)" );
   Console::WriteLine( "  u {0}", myDT.ToString( "u" ) );
   Console::WriteLine( " {0} {1}\n", myDTFI->UniversalSortableDateTimePattern, "(UniversalSortableDateTimePattern)" );
   Console::WriteLine( "  U {0}\n", myDT.ToString( "U" ) );
   Console::WriteLine( "  y {0}", myDT.ToString( "y" ) );
   Console::WriteLine( " {0} {1}\n", myDTFI->YearMonthPattern, "(YearMonthPattern)" );
   Console::WriteLine( "  Y {0}", myDT.ToString( "Y" ) );
   Console::WriteLine( " {0} {1}\n", myDTFI->YearMonthPattern, "(YearMonthPattern)" );
}

/*
This code produces the following output.

FORMAT  en-US EXAMPLE
CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY

d     1/3/2002
M/d/yyyy (ShortDatePattern)

D     Thursday, January 03, 2002
dddd, MMMM dd, yyyy (LongDatePattern)

f     Thursday, January 03, 2002 12:00 AM

F     Thursday, January 03, 2002 12:00:00 AM
dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)

g     1/3/2002 12:00 AM

G     1/3/2002 12:00:00 AM

m     January 03
MMMM dd (MonthDayPattern)

M     January 03
MMMM dd (MonthDayPattern)

r     Thu, 03 Jan 2002 00:00:00 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)

R     Thu, 03 Jan 2002 00:00:00 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)

s     2002-01-03T00:00:00
yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)

t     12:00 AM
h:mm tt (ShortTimePattern)

T     12:00:00 AM
h:mm:ss tt (LongTimePattern)

u     2002-01-03 00:00:00Z
yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)

U     Thursday, January 03, 2002 8:00:00 AM

y     January, 2002
MMMM, yyyy (YearMonthPattern)

Y     January, 2002
MMMM, yyyy (YearMonthPattern)

*/
import System.* ;
import System.Globalization.* ;

public class SamplesDTFI
{
    public static void main(String[] args)
    {
        // Creates and initializes a DateTimeFormatInfo associated with 
        // the en-US culture.
        DateTimeFormatInfo myDTFI = 
            (new CultureInfo("en-US", false)).get_DateTimeFormat();

        // Creates a DateTime with the Gregorian date January 3, 2002 
        // (year=2002, month=1, day=3).
        // The Gregorian calendar is the default calendar for the 
        // en-US culture.
        DateTime myDT =  new DateTime(2002, 1, 3);

        // Displays the format pattern associated with each format character.
        Console.WriteLine("FORMAT  en-US EXAMPLE");
        Console.WriteLine("CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY\n");
        Console.WriteLine("  d     {0}", myDT.ToString("d"));
        Console.WriteLine("        {0} {1}\n", myDTFI.get_ShortDatePattern(),
            "(ShortDatePattern)");
        Console.WriteLine("  D     {0}", myDT.ToString("D"));
        Console.WriteLine("        {0} {1}\n", myDTFI.get_LongDatePattern(), 
            "(LongDatePattern)");
        Console.WriteLine("  f     {0}\n", myDT.ToString("f"));
        Console.WriteLine("  F     {0}", myDT.ToString("F"));
        Console.WriteLine("        {0} {1}\n", myDTFI.get_FullDateTimePattern(),
            "(FullDateTimePattern)");
        Console.WriteLine("  g     {0}\n", myDT.ToString("g"));
        Console.WriteLine("  G     {0}\n", myDT.ToString("G"));
        Console.WriteLine("  m     {0}", myDT.ToString("m"));
        Console.WriteLine("        {0} {1}\n", myDTFI.get_MonthDayPattern(), 
            "(MonthDayPattern)");
        Console.WriteLine("  M     {0}", myDT.ToString("M"));
        Console.WriteLine("        {0} {1}\n", myDTFI.get_MonthDayPattern(), 
            "(MonthDayPattern)");
        Console.WriteLine("  r     {0}", myDT.ToString("r"));
        Console.WriteLine("        {0} {1}\n", myDTFI.get_RFC1123Pattern(),
            "(RFC1123Pattern)");
        Console.WriteLine("  R     {0}", myDT.ToString("R"));
        Console.WriteLine("        {0} {1}\n", myDTFI.get_RFC1123Pattern(), 
            "(RFC1123Pattern)");
        Console.WriteLine("  s     {0}", myDT.ToString("s"));
        Console.WriteLine("        {0} {1}\n", 
            myDTFI.get_SortableDateTimePattern(), "(SortableDateTimePattern)");
        Console.WriteLine("  t     {0}", myDT.ToString("t"));
        Console.WriteLine("        {0} {1}\n", myDTFI.get_ShortTimePattern(),
            "(ShortTimePattern)");
        Console.WriteLine("  T     {0}", myDT.ToString("T"));
        Console.WriteLine("        {0} {1}\n", myDTFI.get_LongTimePattern(), 
            "(LongTimePattern)");
        Console.WriteLine("  u     {0}", myDT.ToString("u"));
        Console.WriteLine("        {0} {1}\n", 
            myDTFI.get_UniversalSortableDateTimePattern(), 
            "(UniversalSortableDateTimePattern)");
        Console.WriteLine("  U     {0}\n", myDT.ToString("U"));
        Console.WriteLine("  y     {0}", myDT.ToString("y"));
        Console.WriteLine("        {0} {1}\n", myDTFI.get_YearMonthPattern(),
            "(YearMonthPattern)");
        Console.WriteLine("  Y     {0}", myDT.ToString("Y"));
        Console.WriteLine("        {0} {1}\n", myDTFI.get_YearMonthPattern(),
            "(YearMonthPattern)");
    } //main 
} //SamplesDTFI

/*
This code produces the following output.

FORMAT  en-US EXAMPLE
CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY

  d     1/3/2002
        M/d/yyyy (ShortDatePattern)

  D     Thursday, January 03, 2002
        dddd, MMMM dd, yyyy (LongDatePattern)

  f     Thursday, January 03, 2002 12:00 AM

  F     Thursday, January 03, 2002 12:00:00 AM
        dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)

  g     1/3/2002 12:00 AM

  G     1/3/2002 12:00:00 AM

  m     January 03
        MMMM dd (MonthDayPattern)

  M     January 03
        MMMM dd (MonthDayPattern)

  r     Thu, 03 Jan 2002 00:00:00 GMT
        ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)

  R     Thu, 03 Jan 2002 00:00:00 GMT
        ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)

  s     2002-01-03T00:00:00
        yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)

  t     12:00 AM
        h:mm tt (ShortTimePattern)

  T     12:00:00 AM
        h:mm:ss tt (LongTimePattern)

  u     2002-01-03 00:00:00Z
        yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)

  U     Thursday, January 03, 2002 8:00:00 AM

  y     January, 2002
        MMMM, yyyy (YearMonthPattern)

  Y     January, 2002
        MMMM, yyyy (YearMonthPattern)
*/

继承层次结构

System.Object
  System.Globalization.DateTimeFormatInfo

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

DateTimeFormatInfo 成员
System.Globalization 命名空间
DateTime
Calendar 类
CultureInfo 类

其他资源

格式化类型
格式化概述
标准 DateTime 格式字符串
自定义 DateTime 格式字符串