tags:

views:

733

answers:

6

I have 2 strings:

string d = "09/06/24";
string t = "13:35:01";

I want to take the strings and combine them to make a datetime variable:

newDT = Convert.ToDateTime(d + t);

Compiles but when it hits that line it fails..........any ideas?

+12  A: 

DateTime.Parse(d + " " + t) should do it, the problem you were probably having is the lack of space inbetween the two variables, you were trying to parse:

"09/06/2413:35:01"

As you can see, this is not a valid date format.

Ed Woodcock
+1  A: 

does this work?

DateTime.Parse(d + " " + t);
blu
A: 

Try:

Convert.ToDateTime(d + " " + t);
Justin Balvanz
A: 

Convert.ToDateTime(d + " " + t) should also work.

Cody C
+1  A: 

Try this:

string d = "09/06/24";
string t = "13:35:01";
DateTime newDT = Convert.ToDateTime(d + " " + t);
AlejandroR
+1  A: 

If you have a specific format of date and time in the string, then consider using DateTime.TryParseExact which allows you to specify one or more formats to use for parsing.

Richard