1

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?

6 Answers 6

13

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.

1

does this work?

DateTime.Parse(d + " " + t); 
1

Try this:

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

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.

0

Try:

Convert.ToDateTime(d + " " + t);
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.