DateTime.GetDateTimeFormats Method

Definition

Converts the value of this instance to all the string representations supported by the standard date and time format specifiers.

Overloads

GetDateTimeFormats()

Converts the value of this instance to all the string representations supported by the standard date and time format specifiers.

GetDateTimeFormats(Char)

Converts the value of this instance to all the string representations supported by the specified standard date and time format specifier.

GetDateTimeFormats(IFormatProvider)

Converts the value of this instance to all the string representations supported by the standard date and time format specifiers and the specified culture-specific formatting information.

GetDateTimeFormats(Char, IFormatProvider)

Converts the value of this instance to all the string representations supported by the specified standard date and time format specifier and culture-specific formatting information.

Remarks

Viktigt

You should not assume that multiple calls to the GetDateTimeFormats overloads will return identical data. Depending on the specific overload, the data returned by this method can change if the current culture changes, the user overrides individual cultural settings, or an update occurs to the system's cultural data.

GetDateTimeFormats()

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

Converts the value of this instance to all the string representations supported by the standard date and time format specifiers.

public string[] GetDateTimeFormats ();

Returns

String[]

A string array where each element is the representation of the value of this instance formatted with one of the standard date and time format specifiers.

Examples

The following example demonstrates the DateTime.GetDateTimeFormats() method. It displays the string representation of a date using all possible standard date and time formats in the computer's current culture, which in this case is en-US.

DateTime july28 = new DateTime(2009, 7, 28, 5, 23, 15, 16);

string[] july28Formats = july28.GetDateTimeFormats();

// Print out july28 in all DateTime formats using the default culture.
foreach (string format in july28Formats) {
    Console.WriteLine(format);
}

The example displays the following output:

7/28/2009
7/28/09
07/28/09
07/28/2009
09/07/28
2009-07-28
28-Jul-09
Tuesday, July 28, 2009
July 28, 2009
Tuesday, 28 July, 2009
28 July, 2009
Tuesday, July 28, 2009 5:23 AM
Tuesday, July 28, 2009 05:23 AM
Tuesday, July 28, 2009 5:23
Tuesday, July 28, 2009 05:23
July 28, 2009 5:23 AM
July 28, 2009 05:23 AM
July 28, 2009 5:23
July 28, 2009 05:23
Tuesday, 28 July, 2009 5:23 AM
Tuesday, 28 July, 2009 05:23 AM
Tuesday, 28 July, 2009 5:23
Tuesday, 28 July, 2009 05:23
28 July, 2009 5:23 AM
28 July, 2009 05:23 AM
28 July, 2009 5:23
28 July, 2009 05:23
Tuesday, July 28, 2009 5:23:15 AM
Tuesday, July 28, 2009 05:23:15 AM
Tuesday, July 28, 2009 5:23:15
Tuesday, July 28, 2009 05:23:15
July 28, 2009 5:23:15 AM
July 28, 2009 05:23:15 AM
July 28, 2009 5:23:15
July 28, 2009 05:23:15
Tuesday, 28 July, 2009 5:23:15 AM
Tuesday, 28 July, 2009 05:23:15 AM
Tuesday, 28 July, 2009 5:23:15
Tuesday, 28 July, 2009 05:23:15
28 July, 2009 5:23:15 AM
28 July, 2009 05:23:15 AM
28 July, 2009 5:23:15
28 July, 2009 05:23:15
7/28/2009 5:23 AM
7/28/2009 05:23 AM
7/28/2009 5:23
7/28/2009 05:23
7/28/09 5:23 AM
7/28/09 05:23 AM
7/28/09 5:23
7/28/09 05:23
07/28/09 5:23 AM
07/28/09 05:23 AM
07/28/09 5:23
07/28/09 05:23
07/28/2009 5:23 AM
07/28/2009 05:23 AM
07/28/2009 5:23
07/28/2009 05:23
09/07/28 5:23 AM
09/07/28 05:23 AM
09/07/28 5:23
09/07/28 05:23
2009-07-28 5:23 AM
2009-07-28 05:23 AM
2009-07-28 5:23
2009-07-28 05:23
28-Jul-09 5:23 AM
28-Jul-09 05:23 AM
28-Jul-09 5:23
28-Jul-09 05:23
7/28/2009 5:23:15 AM
7/28/2009 05:23:15 AM
7/28/2009 5:23:15
7/28/2009 05:23:15
7/28/09 5:23:15 AM
7/28/09 05:23:15 AM
7/28/09 5:23:15
7/28/09 05:23:15
07/28/09 5:23:15 AM
07/28/09 05:23:15 AM
07/28/09 5:23:15
07/28/09 05:23:15
07/28/2009 5:23:15 AM
07/28/2009 05:23:15 AM
07/28/2009 5:23:15
07/28/2009 05:23:15
09/07/28 5:23:15 AM
09/07/28 05:23:15 AM
09/07/28 5:23:15
09/07/28 05:23:15
2009-07-28 5:23:15 AM
2009-07-28 05:23:15 AM
2009-07-28 5:23:15
2009-07-28 05:23:15
28-Jul-09 5:23:15 AM
28-Jul-09 05:23:15 AM
28-Jul-09 5:23:15
28-Jul-09 05:23:15
July 28
July 28
2009-07-28T05:23:15.0160000
2009-07-28T05:23:15.0160000
Tue, 28 Jul 2009 05:23:15 GMT
Tue, 28 Jul 2009 05:23:15 GMT
2009-07-28T05:23:15
5:23 AM
05:23 AM
5:23
05:23
5:23:15 AM
05:23:15 AM
5:23:15
05:23:15
2009-07-28 05:23:15Z
Tuesday, July 28, 2009 12:23:15 PM
Tuesday, July 28, 2009 12:23:15 PM
Tuesday, July 28, 2009 12:23:15
Tuesday, July 28, 2009 12:23:15
July 28, 2009 12:23:15 PM
July 28, 2009 12:23:15 PM
July 28, 2009 12:23:15
July 28, 2009 12:23:15
Tuesday, 28 July, 2009 12:23:15 PM
Tuesday, 28 July, 2009 12:23:15 PM
Tuesday, 28 July, 2009 12:23:15
Tuesday, 28 July, 2009 12:23:15
28 July, 2009 12:23:15 PM
28 July, 2009 12:23:15 PM
28 July, 2009 12:23:15
28 July, 2009 12:23:15
July, 2009
July, 2009

Remarks

The string array returned by the DateTime.GetDateTimeFormats() method is equivalent to combining the string arrays returned by separate calls to the DateTime.GetDateTimeFormats(Char) method with the "d", "D", "f", "F", "g", "G", "m", "o", "r", "s", "t", "T", "u", "U", and "y" standard format strings. For more information about standard format specifiers, see Standard Date and Time Format Strings.

Each element of the return value is formatted using information from the current culture. For more information about culture-specific formatting information for the current culture, see CultureInfo.CurrentCulture.

Viktigt

Because this method uses culture-sensitive data, you should not assume that multiple calls to the method will return identical data. The data returned by this method can change if the current culture changes, the user overrides individual cultural settings, or an update occurs to the system's cultural data.

See also

Applies to

.NET 9 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

GetDateTimeFormats(Char)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

Converts the value of this instance to all the string representations supported by the specified standard date and time format specifier.

public string[] GetDateTimeFormats (char format);

Parameters

format
Char

A standard date and time format string.

Returns

String[]

A string array where each element is the representation of the value of this instance formatted with the format standard date and time format specifier.

Exceptions

format is not a valid standard date and time format specifier character.

Examples

The following example demonstrates the GetDateTimeFormats(Char) method. It displays the string representation of a date using the long date format specifier ("D") for the current culture.

  DateTime july28 = new DateTime(2009, 7, 28, 5, 23, 15);
  
  // Get the long date formats using the current culture.
  string [] longJuly28Formats =
              july28.GetDateTimeFormats('D');

  // Display july28 in all long date formats.
  foreach (string format in longJuly28Formats) {
      Console.WriteLine(format);
  }
// The example displays the following output:
//       Tuesday, July 28, 2009
//       July 28, 2009
//       Tuesday, 28 July, 2009
//       28 July, 2009

Remarks

The format parameter can be any of the standard date and time format specifiers. These include d, D, f, F, g, G, M (or m), O (or o), R (or r), s, t, T, u, U, and Y (or y). For more information, see Standard Date and Time Format Strings.

Each element of the return value is formatted using information from the current culture. For more information about culture-specific formatting information for the current culture, see CultureInfo.CurrentCulture.

Viktigt

Because this method uses culture-sensitive data, you should not assume that multiple calls to the method will return identical data. The data returned by this method can change if the current culture changes, the user overrides individual cultural settings, or an update occurs to the system's cultural data.

See also

Applies to

.NET 9 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

GetDateTimeFormats(IFormatProvider)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

Converts the value of this instance to all the string representations supported by the standard date and time format specifiers and the specified culture-specific formatting information.

public string[] GetDateTimeFormats (IFormatProvider provider);
public string[] GetDateTimeFormats (IFormatProvider? provider);

Parameters

provider
IFormatProvider

An object that supplies culture-specific formatting information about this instance.

Returns

String[]

A string array where each element is the representation of the value of this instance formatted with one of the standard date and time format specifiers.

Examples

The following example demonstrates the DateTime.GetDateTimeFormats(IFormatProvider) method. It displays the string representation of a date using all possible standard date and time formats for the fr-FR culture.

DateTime july28 = new DateTime(2009, 7, 28, 5, 23, 15, 16);

IFormatProvider culture =
    new System.Globalization.CultureInfo("fr-FR", true);
// Get the short date formats using the "fr-FR" culture.
string [] frenchJuly28Formats =
            july28.GetDateTimeFormats(culture);

// Display july28 in various formats using "fr-FR" culture.
foreach (string format in frenchJuly28Formats) {
    Console.WriteLine(format);
}

The example displays the following output:

28/07/2009
28/07/09
28.07.09
28-07-09
2009-07-28
mardi 28 juillet 2009
28 juil. 09
28 juillet 2009
mardi 28 juillet 2009 05:23
mardi 28 juillet 2009 5:23
mardi 28 juillet 2009 05.23
mardi 28 juillet 2009 05 h 23
28 juil. 09 05:23
28 juil. 09 5:23
28 juil. 09 05.23
28 juil. 09 05 h 23
28 juillet 2009 05:23
28 juillet 2009 5:23
28 juillet 2009 05.23
28 juillet 2009 05 h 23
mardi 28 juillet 2009 05:23:15
mardi 28 juillet 2009 5:23:15
mardi 28 juillet 2009 05.23
mardi 28 juillet 2009 05 h 23
28 juil. 09 05:23:15
28 juil. 09 5:23:15
28 juil. 09 05.23
28 juil. 09 05 h 23
28 juillet 2009 05:23:15
28 juillet 2009 5:23:15
28 juillet 2009 05.23
28 juillet 2009 05 h 23
28/07/2009 05:23
28/07/2009 5:23
28/07/2009 05.23
28/07/2009 05 h 23
28/07/09 05:23
28/07/09 5:23
28/07/09 05.23
28/07/09 05 h 23
28.07.09 05:23
28.07.09 5:23
28.07.09 05.23
28.07.09 05 h 23
28-07-09 05:23
28-07-09 5:23
28-07-09 05.23
28-07-09 05 h 23
2009-07-28 05:23
2009-07-28 5:23
2009-07-28 05.23
2009-07-28 05 h 23
28/07/2009 05:23:15
28/07/2009 5:23:15
28/07/2009 05.23
28/07/2009 05 h 23
28/07/09 05:23:15
28/07/09 5:23:15
28/07/09 05.23
28/07/09 05 h 23
28.07.09 05:23:15
28.07.09 5:23:15
28.07.09 05.23
28.07.09 05 h 23
28-07-09 05:23:15
28-07-09 5:23:15
28-07-09 05.23
28-07-09 05 h 23
2009-07-28 05:23:15
2009-07-28 5:23:15
2009-07-28 05.23
2009-07-28 05 h 23
28 juillet
28 juillet
2009-07-28T05:23:15.0160000
2009-07-28T05:23:15.0160000
Tue, 28 Jul 2009 05:23:15 GMT
Tue, 28 Jul 2009 05:23:15 GMT
2009-07-28T05:23:15
05:23
5:23
05.23
05 h 23
05:23:15
5:23:15
05.23
05 h 23
2009-07-28 05:23:15Z
mardi 28 juillet 2009 12:23:15
mardi 28 juillet 2009 12:23:15
mardi 28 juillet 2009 12.23
mardi 28 juillet 2009 12 h 23
28 juil. 09 12:23:15
28 juil. 09 12:23:15
28 juil. 09 12.23
28 juil. 09 12 h 23
28 juillet 2009 12:23:15
28 juillet 2009 12:23:15
28 juillet 2009 12.23
28 juillet 2009 12 h 23
juillet 2009
juillet 2009

Remarks

The string array returned by the DateTime.GetDateTimeFormats(IFormatProvider) method is equivalent to combining the string arrays returned by separate calls to the DateTime.GetDateTimeFormats(Char, IFormatProvider) method with the "d", "D", "f", "F", "g", "G", "m", "o", "r", "s", "t", "T", "u", "U", and "y" standard format strings. For more information about standard format specifiers, see Standard Date and Time Format Strings.

Each element of the return value is formatted using culture-specific information supplied by provider.

Viktigt

Because this method uses culture-sensitive data, you should not assume that multiple calls to the method will return identical data unless provider is a CultureInfo object that represents the invariant culture. The data returned by this method can change if the user overrides the individual cultural settings of provider or if an update occurs to the system's cultural data for provider.

See also

Applies to

.NET 9 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

GetDateTimeFormats(Char, IFormatProvider)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

Converts the value of this instance to all the string representations supported by the specified standard date and time format specifier and culture-specific formatting information.

public string[] GetDateTimeFormats (char format, IFormatProvider provider);
public string[] GetDateTimeFormats (char format, IFormatProvider? provider);

Parameters

format
Char

A date and time format string.

provider
IFormatProvider

An object that supplies culture-specific formatting information about this instance.

Returns

String[]

A string array where each element is the representation of the value of this instance formatted with one of the standard date and time format specifiers.

Exceptions

format is not a valid standard date and time format specifier character.

Examples

The following example demonstrates the GetDateTimeFormats(Char, IFormatProvider) method. It displays the string representations of a date using the short date format specifier ("d") for the fr-FR culture.

  DateTime july28 = new DateTime(2009, 7, 28, 5, 23, 15);
  
  IFormatProvider culture =
      new System.Globalization.CultureInfo("fr-FR", true);
  // Get the short date formats using the "fr-FR" culture.
  string [] frenchJuly28Formats =
              july28.GetDateTimeFormats('d', culture);

  // Display july28 in short date formats using "fr-FR" culture.
  foreach (string format in frenchJuly28Formats) {
      Console.WriteLine(format);
  }
// The example displays the following output:
//       28/07/2009
//       28/07/09
//       28.07.09
//       28-07-09
//       2009-07-28

Remarks

The format parameter can be any of the standard date and time format specifiers. These include d, D, f, F, g, G, M (or m), O (or o), R (or r), s, t, T, u, U, and Y (or y). For more information, see Standard Date and Time Format Strings.

Each element of the return value is formatted using culture-specific information supplied by provider.

Viktigt

Because this method uses culture-sensitive data, you should not assume that multiple calls to the method will return identical data unless provider is a CultureInfo object that represents the invariant culture. The data returned by this method can change if the user overrides the individual cultural settings of provider or if an update occurs to the system's cultural data for provider.

See also

Applies to

.NET 9 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0