Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 421
  • Last Modified:

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.

0
FaheemAhmadGul
Asked:
FaheemAhmadGul
  • 2
  • 2
  • 2
1 Solution
 
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
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
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

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now