String to date time

Dim test As DateTime
        test = DateTime.Parse(startdate.Value.Date & " " & stime.Value.TimeOfDay.ToString)
        MsgBox(test)

Why does this only show the date and not the time?
MillkindAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guru JiCommented:
you can try this

test = DateTime.ParseExact(startdate.Value.Date & " " & stime.Value.TimeOfDay.ToString, "yyyy-MM-dd HH:mm tt",null);
0
MillkindAuthor Commented:
Unable to cast object of type 'System.DBNull' to type 'System.IFormatProvider'.
0
Guru JiCommented:
Can you tell me the values of following if you try to print them on screen

startdate.Value.Date

and

stime.Value.TimeOfDay.ToString
0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

MillkindAuthor Commented:
9/9/2013

00:00:00
0
Guru JiCommented:
can you try this

Sorry I don't have VB on my current system to test,

Dim test as string

Dim myDateTime as DateTime

test = startdate.Value.Date & " " & stime.Value.TimeOfDay.ToString

myDateTime = DateTime.ParseExact(test, "MM\/dd\/yyyy HH:mm:ss", Nothing)


if above doesn't work then try this

Dim test as string

Dim myDateTime as DateTime

test = startdate.Value & " " & stime.Value

myDateTime = DateTime.ParseExact(test, "MM\/dd\/yyyy HH:mm:ss", Nothing)
0
MillkindAuthor Commented:
String was not recognized as a valid DateTime. For the first one.

The second one won't work because the stime.value will return a date.
0
Guru JiCommented:
Once I will have access to VB I will get back to you. Is this VB or VBA ?
0
MillkindAuthor Commented:
vb
0
Guru JiCommented:
Can u please try this

Dim test as string

Dim myDateTime as DateTime

test = startdate.Value.Date & " " & stime.Value.TimeOfDay.ToString

myDateTime = DateTime.ParseExact(test, "M/d/yyyy HH:mm:ss", Nothing)

Console.WriteLine(myDateTime)


I assume in above the test string will return a value of 9/9/2013 00:00:00
0
MillkindAuthor Commented:
Nope only returns the date.
0
Guru JiCommented:
I just ran this and it works flawless

Module VBModule
 
    Sub Main()
        Console.WriteLine("Hello, world!")
        
Dim test as string

Dim myDateTime as DateTime

Dim startdate as string
Dim stime as string

startdate = "9/9/2013"
stime = "00:00:00"

test = startdate & " " & stime
Console.WriteLine(test)
myDateTime = DateTime.ParseExact(test, "M/d/yyyy HH:mm:ss",nothing)

Console.WriteLine(myDateTime)
    End Sub
  
End Module

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Guru JiCommented:
Something wrong with your Stime

you can try Stime.Trim() may be null spaces in it causing some issue
0
MillkindAuthor Commented:
That got it. then boiled down to:

patronusageda.SelectCommand.Parameters.Add(New SqlParameter("@startdate", DateTime.ParseExact(startdate.Value.Date & " " & stime.Value.TimeOfDay.ToString, "M/d/yyyy HH:mm:ss", Nothing)))
0
Guru JiCommented:
Great it worked for you !!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.