VB.NET: How to convert a string to Date?

I have a line going through an SSIS package through a text file of the form:

"20090910" (string) 

and it should be

 2010-09-01 00:00:00 (Date) 

Any suggestions?

+6
date
source share
1 answer

Try DateTime.ParseExact()

An example from MSDN with your data:

 Dim dateString, format As String Dim result As Date Dim provider As Globalization.CultureInfo = Globalization.CultureInfo.InvariantCulture ' Parse date and time with custom specifier. dateString = "20090910" format = "yyyyMMdd" Try result = Date.ParseExact(dateString, format, provider) Console.WriteLine("{0} converts to {1}.", dateString, result.ToString()) Console.ReadLine() Catch e As FormatException Console.WriteLine("{0} is not in the correct format.", dateString) End Try 
+10
source share

All Articles