Visual Basic – Convert String to Date in the correct way

This method of converting a string to a date works regardless of the Region settings on the client machine.

 

Dim dtTestDate As Date
Dim sTestDate As String
sTestDate = "120508" ' May 8, 2012
dtTestDate = Date.ParseExact(sTestDate.Substring(2, 2) & "/" & sTestDate.Substring(4, 2) & "/" & sTestDate.Substring(0, 2), "MM/dd/yy"New System.Globalization.CultureInfo("EN-US"), System.Globalization.DateTimeStyles.None)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s