Mega Code Archive

 
Categories / VB.Net / Date Time
 

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