DateTimeFormatInfo Class
Assembly: mscorlib (in mscorlib.dll)
'Declaration <SerializableAttribute> _ <ComVisibleAttribute(True)> _ Public NotInheritable Class DateTimeFormatInfo Implements ICloneable, IFormatProvider 'Usage Dim instance As DateTimeFormatInfo
/** @attribute SerializableAttribute() */ /** @attribute ComVisibleAttribute(true) */ public final class DateTimeFormatInfo implements ICloneable, IFormatProvider
SerializableAttribute ComVisibleAttribute(true) public final class DateTimeFormatInfo implements ICloneable, IFormatProvider
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 | |
| D | |
| 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 | |
| r, R | |
| s | SortableDateTimePattern (based on ISO 8601) using local time |
| t | |
| T | |
| u | UniversalSortableDateTimePattern using the format for universal time display |
| U | Full date and time (long date and long time) using universal time |
| y, Y |
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, %d | The day of the month. Single-digit days will not have a leading zero. Specify "%d" if the format pattern is not combined with other format patterns. |
| 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, %M | The numeric month. Single-digit months will not have a leading zero. Specify "%M" if the format pattern is not combined with other format patterns. |
| 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, %y | The year without the century. If the year without the century is less than 10, the year is displayed with no leading zero. Specify "%y" if the format pattern is not combined with other format patterns. |
| 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, %h | The hour in a 12-hour clock. Single-digit hours will not have a leading zero. Specify "%h" if the format pattern is not combined with other format patterns. |
| hh | The hour in a 12-hour clock. Single-digit hours will have a leading zero. |
| H, %H | The hour in a 24-hour clock. Single-digit hours will not have a leading zero. Specify "%H" if the format pattern is not combined with other format patterns. |
| HH | The hour in a 24-hour clock. Single-digit hours will have a leading zero. |
| m, %m | The minute. Single-digit minutes will not have a leading zero. Specify "%m" if the format pattern is not combined with other format patterns. |
| mm | The minute. Single-digit minutes will have a leading zero. |
| s, %s | The second. Single-digit seconds will not have a leading zero. Specify "%s" if the format pattern is not combined with other format patterns. |
| ss | The second. Single-digit seconds will have a leading zero. |
| f, %f | The fraction of a second in single-digit precision. The remaining digits are truncated. Specify "%f" if the format pattern is not combined with other format patterns. |
| 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. |
| F, %F | Displays the most significant digit of the seconds fraction. Nothing is displayed if the digit is zero. Specify "%F" if the format pattern is not combined with other format patterns. |
| FF | Displays the two most significant digits of the seconds fraction. However, trailing zeros, or two zero digits, are not displayed. |
| FFF | Displays the three most significant digits of the seconds fraction. However, trailing zeros, or three zero digits, are not displayed. |
| FFFF | Displays the four most significant digits of the seconds fraction. However, trailing zeros, or four zero digits, are not displayed. |
| FFFFF | Displays the five most significant digits of the seconds fraction. However, trailing zeros, or five zero digits, are not displayed. |
| FFFFFF | Displays the six most significant digits of the seconds fraction. However, trailing zeros, or six zero digits, are not displayed. |
| FFFFFFF | Displays the seven most significant digits of the seconds fraction. However, trailing zeros, or seven zero digits, are not displayed. |
| t, %t | The first character in the AM/PM designator defined in AMDesignator or PMDesignator, if any. Specify "%t" if the format pattern is not combined with other format patterns. |
| tt | The AM/PM designator defined in AMDesignator or PMDesignator, if any. |
| z, %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". Specify "%z" if the format pattern is not combined with other format patterns. |
| 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. That is, to use format pattern "d", "f", "F", "h", "m", "s", "t", "y", "z", "H", or "M" by itself, specify "%d", "%f", "%F", "%h", "%m", "%s", "%t", "%y", "%z", "%H", or "%M". 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.
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.
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) '
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)
*/
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
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.