Solved

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

Posted on 2010-09-16
6
411 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Title # Comments Views Activity
Automatic Email Reminder 4 37
Database Connections Not being returned to Connection Pool 7 25
C# Gridview 1 32
Formula for calculating ROI on training 4 20
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

773 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