Matthew
Matthew

Reputation: 63

VB.NET: How to convert string to Date?

I have string coming in through an SSIS package via text file in form:

"20090910" (string)

and it needs to be

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

Any suggestions?

Upvotes: 6

Views: 37821

Answers (1)

p.campbell
p.campbell

Reputation: 100637

Try DateTime.ParseExact()

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 

Upvotes: 11

Related Questions