Solved

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

Posted on 2010-09-16
6
412 Views
Last Modified: 2013-11-26
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.

0
Comment
Question by:FaheemAhmadGul
  • 2
  • 2
  • 2
6 Comments
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 125 total points
ID: 33696659
mydate=Convert.ToDateTime(myString)
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33696694
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
 

Author Closing Comment

by:FaheemAhmadGul
ID: 33696716
Many thanks.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:FaheemAhmadGul
ID: 33696737
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
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33696757
Not a problem. We all learn.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33696775
glad i helped you too...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question