Solved

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

Posted on 2010-09-16
6
413 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

738 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