Link to home
Start Free TrialLog in
Avatar of Millkind
MillkindFlag for Afghanistan

asked on

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?
Avatar of Guru Ji
Guru Ji
Flag of Canada image

you can try this

test = DateTime.ParseExact(startdate.Value.Date & " " & stime.Value.TimeOfDay.ToString, "yyyy-MM-dd HH:mm tt",null);
Avatar of Millkind

ASKER

Unable to cast object of type 'System.DBNull' to type 'System.IFormatProvider'.
Can you tell me the values of following if you try to print them on screen

startdate.Value.Date

and

stime.Value.TimeOfDay.ToString
9/9/2013

00:00:00
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)
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.
Once I will have access to VB I will get back to you. Is this VB or VBA ?
vb
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
Nope only returns the date.
ASKER CERTIFIED SOLUTION
Avatar of Guru Ji
Guru Ji
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Something wrong with your Stime

you can try Stime.Trim() may be null spaces in it causing some issue
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)))
Great it worked for you !!!