user2082630
user2082630

Reputation: 279

How to combine two strings (date and time) to a single DateTime

I have two strings:

string one = "13/02/09";
string two = "2:35:10 PM";

I want to combine these two together and convert to a DateTime.

I tried the following but it doesn't work:

DateTime dt = Convert.ToDateTime(one + " " + two);
DateTime dt1 = DateTime.ParseExact(one + " " + two, "dd/MM/yy HH:mm:ss tt", CultureInfo.InvariantCulture);

What can I do to make this work?

Upvotes: 15

Views: 47833

Answers (9)

Divya Agrawal
Divya Agrawal

Reputation: 285

I had different format and the above answer did not work:

string one = "2019-02-06";
string two = "18:30";

The solution for this format is:

DateTime newDateTime = Convert.ToDateTime(one).Add(TimeSpan.Parse(two));

The result will be: newDateTime{06-02-2019 18:30:00}

Upvotes: 3

Stephane Delcroix
Stephane Delcroix

Reputation: 16232

use DateTime.Parse () to parse the date and the time separately. Then add the time component of the second one to the first one, like this

var date = DateTime.Parse (one);
var time = DateTime.Parse (two);
var result = date + time - time.Date;

Upvotes: -1

Chris S
Chris S

Reputation: 65436

Convert.ToDateTime uses DateTime.ParseExact with your current thread's culture, so you can make things a bit clearer by simply doing:

string date = "13/02/09";
string time = "2:35:10 PM";
DateTime dateTime = DateTime.Parse(date +" "+ time, new CultureInfo("en-GB"));
Console.WriteLine (dateTime);

That gives the result 13/02/2009 14:35:10, and forces the parse to use the en-GB date time formats. If your Windows installation is en-GB anyway, you don't need the CultureInfo(..) argument.

Upvotes: 0

Petar Ivanov
Petar Ivanov

Reputation: 93040

The problem is that the format string that you specify is not correct.

'HH' means a dwo-digit hour, but you have a single digit hour.

Use 'h' instead.

So the full format is 'dd/MM/yy h:mm:ss tt'

Upvotes: 1

Soner Gönül
Soner Gönül

Reputation: 98760

Try like this;

string one = "13/02/09";
string two = "2:35:10 PM";

DateTime dt = Convert.ToDateTime(one + " " + two);
DateTime dt1 = DateTime.ParseExact(one + " " + two, "dd/MM/yy h:mm:ss tt", CultureInfo.InvariantCulture);

Console.WriteLine(dt1);

Here is a DEMO.

HH using a 24-hour clock from 00 to 23. For example; 1:45:30 AM -> 01 and 1:45:30 PM -> 13

h using a 12-hour clock from 1 to 12. For example; 1:45:30 AM -> 1 and 1:45:30 PM -> 1

Check out for more information Custom Date and Time Format Strings

Upvotes: 16

Ryszard Dżegan
Ryszard Dżegan

Reputation: 25434

Use string two = "02:35:10 PM"; instead of string two = "2:35:10 PM"; and also hh instead of HH due to AM/PM format.

Below is the code:

string one = "13/02/09";
string two = "02:35:10 PM";

DateTime dateTime = DateTime.ParseExact(one + " " + two, "dd/MM/yy hh:mm:ss tt", CultureInfo.InvariantCulture);

Upvotes: 0

Alex Filipovici
Alex Filipovici

Reputation: 32561

Try using a culture info which matches the DateTime format for your string values:

DateTime dt = Convert.ToDateTime(one + " " + two,
    CultureInfo.GetCultureInfo("ro-RO"));

or modify the input string so that the hour has 2 digits:

string one = "13/02/09";
string two = "02:35:10 PM";
DateTime dt1 = DateTime.ParseExact(one + " " + two, 
    "dd/MM/yy HH:mm:ss tt",
    CultureInfo.InvariantCulture);

Upvotes: 2

Kesty
Kesty

Reputation: 620

The following code will do what you want. I used the UK culture to take care of the d/m/y structure of your date:

        string string1 = "13/2/09";
        string string2 = "2:35:10 PM";
        DateTime combined = DateTime.Parse(string1 + ' ' + string2, new CultureInfo("UK"));

Upvotes: 0

Rawling
Rawling

Reputation: 50114

Your issue is with your hour specifier; you want h (The hour, using a 12-hour clock from 1 to 12), not HH (The hour, using a 24-hour clock from 00 to 23).

Upvotes: 3

Related Questions