Export (0) Print
Expand All

DateTimeFormatInfo.GetAllDateTimePatterns Method

Returns all the standard patterns in which date and time values can be formatted.

Namespace:  System.Globalization
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Function GetAllDateTimePatterns As String()

Return Value

Type: System.String()
An array that contains the standard patterns in which date and time values can be formatted.

This method returns an array that contains all custom format strings that correspond to standard format strings. See Standard Date and Time Format Strings for a list of the standard format strings.

You can use the custom format strings in the array returned by the GetAllDateTimePatterns method in formatting operations. However, if you do, the string representation of a date and time value returned in that formatting operation cannot always be parsed successfully by the Parse and TryParse methods. Therefore, you cannot assume that the custom format strings returned by the GetAllDateTimePatterns method can be used to round-trip date and time values. The following example illustrates this problem. It retrieves a DateTimeFormatInfo object that contains formatting information for the Italy (Italian) culture. It passes each custom format string in the array returned by the GetAllDateTimePatterns method to the DateTime.ToString(String) method to create the string representation of a date and time. This example then attempts to parse this value by calling the DateTime.TryParse(String, DateTime) method. As the output from the example shows, some of the custom format strings do not produce a date and time value that successfully round-trips.

Imports System.Globalization

Module Example
   Public Sub Main()
      Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture("it-IT")
      Dim date1 = New DateTime(2011, 02, 01, 7, 30, 45, 0)
      Dim date2 As DateTime
      Dim total, noRoundTrip As Integer 

      For Each fmt In culture.DateTimeFormat.GetAllDateTimePatterns()
         total += 1
         If Not DateTime.TryParse(date1.ToString(fmt), date2)
            noRoundTrip += 1
            Console.WriteLine("Unable to parse {0:" + fmt + "} (format '{1}')", 
                              date1, fmt)
         End If              
      Next
      Console.WriteLine()
      Console.WriteLine("Unable to round-trip {0} of {1} format strings.",
                        noRoundTrip, total)
   End Sub 
End Module 
' The example displays the following output: 
'    Unable to parse Tuesday 1 February 2011 7.30 (format 'dddd d MMMM yyyy H.mm') 
'    Unable to parse Tuesday 1 February 2011 07.30 (format 'dddd d MMMM yyyy HH.mm') 
'    Unable to parse 1-Feb-11 7.30 (format 'd-MMM-yy H.mm') 
'    Unable to parse 1-Feb-11 07.30 (format 'd-MMM-yy HH.mm') 
'    Unable to parse 1 February 2011 7.30 (format 'd MMMM yyyy H.mm') 
'    Unable to parse 1 February 2011 07.30 (format 'd MMMM yyyy HH.mm') 
'    Unable to parse Tuesday 1 February 2011 7.30.45 (format 'dddd d MMMM yyyy H.mm.ss') 
'    Unable to parse Tuesday 1 February 2011 07.30.45 (format 'dddd d MMMM yyyy HH.mm.ss') 
'    Unable to parse 1-Feb-11 7.30.45 (format 'd-MMM-yy H.mm.ss') 
'    Unable to parse 1-Feb-11 07.30.45 (format 'd-MMM-yy HH.mm.ss') 
'    Unable to parse 1 February 2011 7.30.45 (format 'd MMMM yyyy H.mm.ss') 
'    Unable to parse 1 February 2011 07.30.45 (format 'd MMMM yyyy HH.mm.ss') 
'    Unable to parse 01/02/2011 7.30 (format 'dd/MM/yyyy H.mm') 
'    Unable to parse 01/02/2011 07.30 (format 'dd/MM/yyyy HH.mm') 
'    Unable to parse 01/Feb/2011 7.30 (format 'dd/MMM/yyyy H.mm') 
'    Unable to parse 01/Feb/2011 07.30 (format 'dd/MMM/yyyy HH.mm') 
'    Unable to parse 01/02/11 7.30 (format 'dd/MM/yy H.mm') 
'    Unable to parse 01/02/11 07.30 (format 'dd/MM/yy HH.mm') 
'    Unable to parse 01.2.11 7.30 (format 'dd.M.yy H.mm') 
'    Unable to parse 01.2.11 07.30 (format 'dd.M.yy HH.mm') 
'    Unable to parse 1/2/11 7.30 (format 'd/M/yy H.mm') 
'    Unable to parse 1/2/11 07.30 (format 'd/M/yy HH.mm') 
'    Unable to parse 2011-02-01 7.30 (format 'yyyy-MM-dd H.mm') 
'    Unable to parse 2011-02-01 07.30 (format 'yyyy-MM-dd HH.mm') 
'    Unable to parse 01/02/2011 7.30.45 (format 'dd/MM/yyyy H.mm.ss') 
'    Unable to parse 01/02/2011 07.30.45 (format 'dd/MM/yyyy HH.mm.ss') 
'    Unable to parse 01/Feb/2011 7.30.45 (format 'dd/MMM/yyyy H.mm.ss') 
'    Unable to parse 01/Feb/2011 07.30.45 (format 'dd/MMM/yyyy HH.mm.ss') 
'    Unable to parse 01/02/11 7.30.45 (format 'dd/MM/yy H.mm.ss') 
'    Unable to parse 01/02/11 07.30.45 (format 'dd/MM/yy HH.mm.ss') 
'    Unable to parse 01.2.11 7.30.45 (format 'dd.M.yy H.mm.ss') 
'    Unable to parse 01.2.11 07.30.45 (format 'dd.M.yy HH.mm.ss') 
'    Unable to parse 1/2/11 7.30.45 (format 'd/M/yy H.mm.ss') 
'    Unable to parse 1/2/11 07.30.45 (format 'd/M/yy HH.mm.ss') 
'    Unable to parse 2011-02-01 7.30.45 (format 'yyyy-MM-dd H.mm.ss') 
'    Unable to parse 2011-02-01 07.30.45 (format 'yyyy-MM-dd HH.mm.ss') 
'    Unable to parse Tuesday 1 February 2011 7.30.45 (format 'dddd d MMMM yyyy H.mm.ss') 
'    Unable to parse Tuesday 1 February 2011 07.30.45 (format 'dddd d MMMM yyyy HH.mm.ss') 
'    Unable to parse 1-Feb-11 7.30.45 (format 'd-MMM-yy H.mm.ss') 
'    Unable to parse 1-Feb-11 07.30.45 (format 'd-MMM-yy HH.mm.ss') 
'    Unable to parse 1 February 2011 7.30.45 (format 'd MMMM yyyy H.mm.ss') 
'    Unable to parse 1 February 2011 07.30.45 (format 'd MMMM yyyy HH.mm.ss') 
'     
'    Unable to round-trip 42 of 98 format strings.

To parse the string representation of a date and time that can be expressed in a number of predefined custom formats, call one of the following methods:

The custom format strings in the array returned by the GetAllDateTimePatterns method depends on the current calendar of the culture for which the DateTimeFormatInfo object supplies formatting information. If the calendar changes, the array returned by this method also changes.

The following example displays the date and time format strings for the invariant culture, as well as the result string that is produced when that format string is used to format a particular date.

Imports System.Globalization

Module Example
   Public Sub Main()
      Dim date1 As Date = #08/28/2014 12:28:30PM#
      Dim invDTF As New DateTimeFormatInfo()
      Dim formats() As String = invDTF.GetAllDateTimePatterns()

      Console.WriteLine("{0,-40} {1}", "Pattern", "Result String")
      Console.WriteLine()
      For Each fmt In formats
         Console.WriteLine("{0,-40} {1}", fmt, date1.ToString(fmt))
      Next 
   End Sub 
End Module 
' The example displays the following output: 
'    Pattern                                  Result String 

'    MM/dd/yyyy                               08/28/2014 
'    yyyy-MM-dd                               2014-08-28 
'    dddd, dd MMMM yyyy                       Thursday, 28 August 2014 
'    dddd, dd MMMM yyyy HH:mm                 Thursday, 28 August 2014 12:28 
'    dddd, dd MMMM yyyy hh:mm tt              Thursday, 28 August 2014 12:28 PM 
'    dddd, dd MMMM yyyy H:mm                  Thursday, 28 August 2014 12:28 
'    dddd, dd MMMM yyyy h:mm tt               Thursday, 28 August 2014 12:28 PM 
'    dddd, dd MMMM yyyy HH:mm:ss              Thursday, 28 August 2014 12:28:30 
'    MM/dd/yyyy HH:mm                         08/28/2014 12:28 
'    MM/dd/yyyy hh:mm tt                      08/28/2014 12:28 PM 
'    MM/dd/yyyy H:mm                          08/28/2014 12:28 
'    MM/dd/yyyy h:mm tt                       08/28/2014 12:28 PM 
'    yyyy-MM-dd HH:mm                         2014-08-28 12:28 
'    yyyy-MM-dd hh:mm tt                      2014-08-28 12:28 PM 
'    yyyy-MM-dd H:mm                          2014-08-28 12:28 
'    yyyy-MM-dd h:mm tt                       2014-08-28 12:28 PM 
'    MM/dd/yyyy HH:mm:ss                      08/28/2014 12:28:30 
'    yyyy-MM-dd HH:mm:ss                      2014-08-28 12:28:30 
'    MMMM dd                                  August 28 
'    MMMM dd                                  August 28 
'    yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK   2014-08-28T12:28:30.0000000 
'    yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK   2014-08-28T12:28:30.0000000 
'    ddd, dd MMM yyyy HH':'mm':'ss 'GMT'      Thu, 28 Aug 2014 12:28:30 GMT 
'    ddd, dd MMM yyyy HH':'mm':'ss 'GMT'      Thu, 28 Aug 2014 12:28:30 GMT 
'    yyyy'-'MM'-'dd'T'HH':'mm':'ss            2014-08-28T12:28:30 
'    HH:mm                                    12:28 
'    hh:mm tt                                 12:28 PM 
'    H:mm                                     12:28 
'    h:mm tt                                  12:28 PM 
'    HH:mm:ss                                 12:28:30 
'    yyyy'-'MM'-'dd HH':'mm':'ss'Z'           2014-08-28 12:28:30Z 
'    dddd, dd MMMM yyyy HH:mm:ss              Thursday, 28 August 2014 12:28:30 
'    yyyy MMMM                                2014 August 
'    yyyy MMMM                                2014 August

The example instantiates a DateTimeFormatInfo object that represents the invariant culture by calling the DateTimeFormatInfo constructor. It could also retrieve a DateTimeFormatInfo that represents the invariant culture from the InvariantInfo property.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show:
© 2015 Microsoft