Solved

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

Posted on 2010-09-16
6
414 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
 
LVL 1

Author Closing Comment

by:FaheemAhmadGul
ID: 33696716
Many thanks.
0
Industry Leaders: 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!

 
LVL 1

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 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