How to convert a String Variable to a DateTime Variable in Visual Basic

I have a string variable myString that is actually holding a Date and Time. I would like to convert this string Variable to a DateTime variable name myDate,

I have declared the variables as below

Dim myDate as DateTime

Dim myString as String

myString = "09/02/2010 22:10:41"

I would now like to convert the value contained in myString to a DateTime Variable and assign this value to  myDate.
I would be grateful for help with this. Please not I need this for a Visual Basic Project in Visual Studio 2008. Thank you for your help in anticipation.

LVL 1
FaheemAhmadGulAsked:
Who is Participating?
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
mydate=Convert.ToDateTime(myString)
0
 
ZhaolaiCommented:
You may want to validate the string before converting, like this:

        Dim myDate As DateTime
        Dim myString As String
        myString = "09/02/2010 22:10:41"
        If IsDate(myString) Then
            myDate = Convert.ToDateTime(myString)
        End If

Open in new window

0
 
FaheemAhmadGulAuthor Commented:
Many thanks.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
FaheemAhmadGulAuthor Commented:
Hi Zhaolai:
I had just accepted the solution posted earlier when I saw your solution which is also correct. I thank you for your solution as it is also helpful to me from my learning point of view.
0
 
ZhaolaiCommented:
Not a problem. We all learn.
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
glad i helped you too...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.