Solved

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

Posted on 2010-09-16
6
408 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now