Parse Date with different patterns : Date Time Parse « Date Time « VB.Net






Parse Date with different patterns

 

Imports System.Globalization

Module Example
   Public Sub Main()
      Dim dateValues() As String = { "30-12-2011", "12-30-2011", 
                                      "30-12-11", "12-30-11" }
      Dim pattern As String = "MM-dd-yy"
      Dim parsedDate As Date

      For Each dateValue As String In dateValues
         If DateTime.TryParseExact(dateValue, pattern, Nothing, 
                                   DateTimeStyles.None, parsedDate) Then
            Console.WriteLine("Converted '{0}' to {1:d}.", dateValue, parsedDate)
         Else
            Console.WriteLine("Unable to convert '{0}' to a date and time.", dateValue)
         End If                                                         
      Next
   End Sub
End Module

   
  








Related examples in the same category

1.Parse string to create new TimeSpan
2.Parse date with no style flags
3.Use custom formats with M and MM to parse a string to Date
4.Parse a string with time zone information
5.Parse a string represengting UTC
6.Parse the same date and time with the AssumeLocal style
7.Parse a date and time that is assumed to be local.
8.Parse String to Date with Invariant Culture
9.Parse string like '2/16/2008 12:15:12 PM' to Date
10.Call overload of Parse to convert string formatted according to conventions of fr-FR culture
11.Parse string with date but no time component
12.Parse a date and time with no styles
13.Parse the same date and time with the AssumeLocal style
14.Parse a date and time that is assumed to be local
15.Parse date-only value without leading zero in month using "d" format.
16.Parse date and time with custom specifier
17.Parse date and time with offset but without offset's minutes
18.Parse string to Date with g format and fr-FR culture
19.Parse String to Integer and catch OverflowException
20.Parse String to Date by using different culture format