Parse date with no style flags
Imports System
Imports System.Text
Imports Microsoft.VisualBasic.Strings
Imports System.Globalization
Class MainClass
Public Shared Sub Main()
Dim enUS As New CultureInfo("en-US")
Dim dateString As String
Dim dateValue As Date
dateString = " 5/01/2009 8:30 AM"
Try
dateValue = Date.ParseExact(dateString, "g", enUS, DateTimeStyles.None)
Console.WriteLine(dateValue)
Catch e As FormatException
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End Try
Try
dateValue = Date.ParseExact(dateString, "g", enUS, DateTimeStyles.AllowLeadingWhite)
Console.WriteLine(dateValue)
Catch e As FormatException
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End Try
End Sub
End Class
Related examples in the same category