Export (0) Print
Expand All

DateTimeFormatInfo Class

Defines how DateTime values are formatted and displayed, depending on the culture.

For a list of all members of this type, see DateTimeFormatInfo Members.

System.Object
   System.Globalization.DateTimeFormatInfo

[Visual Basic]
<Serializable>
NotInheritable Public Class DateTimeFormatInfo
   Implements ICloneable, IFormatProvider
[C#]
[Serializable]
public sealed class DateTimeFormatInfo : ICloneable,
   IFormatProvider
[C++]
[Serializable]
public __gc __sealed class DateTimeFormatInfo : public ICloneable,
   IFormatProvider
[JScript]
public
   Serializable
class DateTimeFormatInfo implements ICloneable,
   IFormatProvider

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

This class contains information, such as date patterns, time patterns, and AM/PM designators.

To create a DateTimeFormatInfo for a specific culture, create a CultureInfo for that culture and retrieve the CultureInfo.DateTimeFormat property. To create a DateTimeFormatInfo for the culture of the current thread, use the CurrentInfo property. To create a DateTimeFormatInfo for the invariant culture, use the InvariantInfo property for a read-only version, or use the DateTimeFormatInfo constructor for a writable version. It is not possible to create a DateTimeFormatInfo for a neutral culture.

The user might choose to override some of the values associated with the current culture of Windows through Regional and Language Options (or Regional Options or Regional Settings) in Control Panel. For example, the user might choose to display the date in a different format or to use a currency other than the default for the culture. If the CultureInfo.UseUserOverride property is set to true, the properties of the CultureInfo.DateTimeFormat instance, the CultureInfo.NumberFormat instance, and the CultureInfo.TextInfo instance are also retrieved from the user settings. If the user settings are incompatible with the culture associated with the CultureInfo (for example, if the selected calendar is not one of the OptionalCalendars), the results of the methods and the values of the properties are undefined.

DateTime values are formatted using standard or custom patterns stored in the properties of a DateTimeFormatInfo.

The standard patterns can be replaced with custom patterns by setting the associated properties of a writable DateTimeFormatInfo. To determine if a DateTimeFormatInfo is writable, use the IsReadOnly property.

The following table lists the standard format characters for each standard pattern and the associated DateTimeFormatInfo property that can be set to modify the standard pattern. The format characters are case-sensitive; for example, 'g' and 'G' represent slightly different patterns.

Format Character Associated Property/ Description
d ShortDatePattern
D LongDatePattern
f Full date and time (long date and short time)
F FullDateTimePattern (long date and long time)
g General (short date and short time)
G General (short date and long time)
m, M MonthDayPattern
r, R RFC1123Pattern
s SortableDateTimePattern (based on ISO 8601) using local time
t ShortTimePattern
T LongTimePattern
u UniversalSortableDateTimePattern using the format for universal time display
U Full date and time (long date and long time) using universal time
y, Y YearMonthPattern

The following table lists the patterns that can be combined to construct custom patterns. The patterns are case-sensitive; for example, "MM" is recognized, but "mm" is not. If the custom pattern contains white-space characters or characters enclosed in single quotation marks, the output string will also contain those characters. Characters not defined as part of a format pattern or as format characters are reproduced literally.

Format Pattern Description
d The day of the month. Single-digit days will not have a leading zero.
dd The day of the month. Single-digit days will have a leading zero.
ddd The abbreviated name of the day of the week, as defined in AbbreviatedDayNames.
dddd The full name of the day of the week, as defined in DayNames.
M The numeric month. Single-digit months will not have a leading zero.
MM The numeric month. Single-digit months will have a leading zero.
MMM The abbreviated name of the month, as defined in AbbreviatedMonthNames.
MMMM The full name of the month, as defined in MonthNames.
y The year without the century. If the year without the century is less than 10, the year is displayed with no leading zero.
yy The year without the century. If the year without the century is less than 10, the year is displayed with a leading zero.
yyyy The year in four digits, including the century.
gg The period or era. This pattern is ignored if the date to be formatted does not have an associated period or era string.
h The hour in a 12-hour clock. Single-digit hours will not have a leading zero.
hh The hour in a 12-hour clock. Single-digit hours will have a leading zero.
H The hour in a 24-hour clock. Single-digit hours will not have a leading zero.
HH The hour in a 24-hour clock. Single-digit hours will have a leading zero.
m The minute. Single-digit minutes will not have a leading zero.
mm The minute. Single-digit minutes will have a leading zero.
s The second. Single-digit seconds will not have a leading zero.
ss The second. Single-digit seconds will have a leading zero.
f The fraction of a second in single-digit precision. The remaining digits are truncated.
ff The fraction of a second in double-digit precision. The remaining digits are truncated.
fff The fraction of a second in three-digit precision. The remaining digits are truncated.
ffff The fraction of a second in four-digit precision. The remaining digits are truncated.
fffff The fraction of a second in five-digit precision. The remaining digits are truncated.
ffffff The fraction of a second in six-digit precision. The remaining digits are truncated.
fffffff The fraction of a second in seven-digit precision. The remaining digits are truncated.
t The first character in the AM/PM designator defined in AMDesignator or PMDesignator, if any.
tt The AM/PM designator defined in AMDesignator or PMDesignator, if any.
z The time zone offset ("+" or "-" followed by the hour only). Single-digit hours will not have a leading zero. For example, Pacific Standard Time is "-8".
zz The time zone offset ("+" or "-" followed by the hour only). Single-digit hours will have a leading zero. For example, Pacific Standard Time is "-08".
zzz The full time zone offset ("+" or "-" followed by the hour and minutes). Single-digit hours and minutes will have leading zeros. For example, Pacific Standard Time is "-08:00".
: The default time separator defined in TimeSeparator.
/ The default date separator defined in DateSeparator.
% c Where c is a format pattern if used alone. The "%" character can be omitted if the format pattern is combined with literal characters or other format patterns.
\ c Where c is any character. Displays the character literally. To display the backslash character, use "\\".

Only format patterns listed in the second table above can be used to create custom patterns; standard format characters listed in the first table cannot be used to create custom patterns. Custom patterns are at least two characters long; for example,

  • DateTime.ToString( "d") returns the DateTime value; "d" is the standard short date pattern.
  • DateTime.ToString( "%d") returns the day of the month; "%d" is a custom pattern.
  • DateTime.ToString( "d ") returns the day of the month followed by a white-space character; "d " is a custom pattern.

A DateTimeFormatInfo or a NumberFormatInfo can be created only for the invariant culture or for specific cultures, not for neutral cultures. For more information about the invariant culture, specific cultures, and neutral cultures, see the CultureInfo class.

This class implements the ICloneable interface to enable duplication of DateTimeFormatInfo objects. It also implements IFormatProvider to supply formatting information to applications.

Example

[Visual Basic, C#, C++] The following code example prints out the different format patterns for the en-US culture using the format characters. It also displays the value of the properties associated with the format character.

[Visual Basic] 
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)
'

[C#] 
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)

*/

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;

int main()
{
   // Creates and initializes a DateTimeFormatInfo associated with the en-US culture.
   CultureInfo * MyCI = new CultureInfo(S"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(S"FORMAT  en-US EXAMPLE");
   Console::WriteLine(S"CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY\n");
   Console::WriteLine(S"  d {0}", myDT.ToString(S"d"));
   Console::WriteLine(S" {0} {1}\n", myDTFI -> ShortDatePattern, S"(ShortDatePattern)");
   Console::WriteLine(S"  D {0}", myDT.ToString(S"D"));
   Console::WriteLine(S" {0} {1}\n", myDTFI -> LongDatePattern, S"(LongDatePattern)");
   Console::WriteLine(S"  f {0}\n", myDT.ToString(S"f"));
   Console::WriteLine(S"  F {0}", myDT.ToString(S"F"));
   Console::WriteLine(S" {0} {1}\n", myDTFI -> FullDateTimePattern, S"(FullDateTimePattern)");
   Console::WriteLine(S"  g {0}\n", myDT.ToString(S"g"));
   Console::WriteLine(S"  G {0}\n", myDT.ToString(S"G"));
   Console::WriteLine(S"  m {0}", myDT.ToString(S"m"));
   Console::WriteLine(S" {0} {1}\n", myDTFI -> MonthDayPattern, S"(MonthDayPattern)");
   Console::WriteLine(S"  M {0}", myDT.ToString(S"M"));
   Console::WriteLine(S" {0} {1}\n", myDTFI -> MonthDayPattern, S"(MonthDayPattern)");
   Console::WriteLine(S"  r {0}", myDT.ToString(S"r"));
   Console::WriteLine(S" {0} {1}\n", myDTFI -> RFC1123Pattern, S"(RFC1123Pattern)");
   Console::WriteLine(S"  R {0}", myDT.ToString(S"R"));
   Console::WriteLine(S" {0} {1}\n", myDTFI -> RFC1123Pattern, S"(RFC1123Pattern)");
   Console::WriteLine(S"  s {0}", myDT.ToString(S"s"));
   Console::WriteLine(S" {0} {1}\n", myDTFI -> SortableDateTimePattern, S"(SortableDateTimePattern)");
   Console::WriteLine(S"  t {0}", myDT.ToString(S"t"));
   Console::WriteLine(S" {0} {1}\n", myDTFI -> ShortTimePattern, S"(ShortTimePattern)");
   Console::WriteLine(S"  T {0}", myDT.ToString(S"T"));
   Console::WriteLine(S" {0} {1}\n", myDTFI -> LongTimePattern, S"(LongTimePattern)");
   Console::WriteLine(S"  u {0}", myDT.ToString(S"u"));
   Console::WriteLine(S" {0} {1}\n", myDTFI -> UniversalSortableDateTimePattern, S"(UniversalSortableDateTimePattern)");
   Console::WriteLine(S"  U {0}\n", myDT.ToString(S"U"));
   Console::WriteLine(S"  y {0}", myDT.ToString(S"y"));
   Console::WriteLine(S" {0} {1}\n", myDTFI -> YearMonthPattern, S"(YearMonthPattern)");
   Console::WriteLine(S"  Y {0}", myDT.ToString(S"Y"));
   Console::WriteLine(S" {0} {1}\n", myDTFI -> YearMonthPattern, S"(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)

*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Globalization

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

DateTimeFormatInfo Members | System.Globalization Namespace | DateTime | Calendar | CultureInfo | Formatting Overview

Show:
© 2014 Microsoft