Willy
Willy

Reputation: 10652

Join date and time strings into a DateTime

Given two strings with the following values:

31/05/2013 0:00:00
21:22

What's the most efficient way to join them into a DateTime data type to get:

31/05/2013 21:22

The time portion of the first string "0:00:00" is ignored, in favor of using the "time" from the second string.

Upvotes: 3

Views: 268

Answers (8)

George Johnston
George Johnston

Reputation: 32258

Use a TimeSpan object and DateTime.Add(yourTimeSpan); e.g.

DateTime dt = new DateTime(2013,05,31);
var dts = dt.Add(new TimeSpan(0, 21, 22, 0, 0));

Extending the answer a bit, you can parse the date and time first, e.g.

DateTime dt = DateTime.Parse("05/31/2013 0:00:00");
TimeSpan ts = TimeSpan.Parse("21:22");
var dts = dt.Add(ts);

...keep in mind, I am not checking for bad date/time values. If you're unsure if the values are real dates/times, use DateTime.TryParse and handle appropriately.

Upvotes: 8

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186748

  DateTime date = DateTime.ParseExact("31/05/2013 0:00:00", "dd/MM/yyyy h:mm:ss", CultureInfo.InvariantCulture);
  TimeSpan span = TimeSpan.ParseExact("21:22", "t", CultureInfo.InvariantCulture);
  DateTime result = date + span;

Upvotes: 0

p.s.w.g
p.s.w.g

Reputation: 149020

If you're starting out with just strings, you can just do this:

var dateString = "31/05/2013 00:00";
var timeString = "21:22";
var dateTimeString = dateString.Substring(0, 11) + timeString;
var output = DateTime.ParseExact(dateTimeString, "dd/MM/yyyy HH:mm", null);

Assuming you know for sure this format won't change (a dangerous assumption, to be sure), this will work. Otherwise, you'd have to parse the date and time strings separately and use conventional date manipulation as others suggested. For example:

var ci = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
var dateString = "31/05/2013 00:00";
var timeString = "21:22";
var output = DateTime.Parse(dateString, ci) + TimeSpan.Parse(timeString, ci);

Upvotes: 0

Yaur
Yaur

Reputation: 7452

If you can safely assume you are getting 2 digit month and day, a 4 digit year, and a space after the date:

var date = "31/05/2013 0:00:00";
var time = "21:22";
var dateTime = DateTime.Parse(date.Substring(0,11) + time);

If the assumptions about the input format aren't solid you could use a regex to extract the date instead of Substring.

Upvotes: 0

Idle_Mind
Idle_Mind

Reputation: 39132

If you are really dealing with only strings, then:

string strDate = "31/05/2013 0:00:00";
string strTime = "21:22";
string strDateTime = strDate.Split(' ')[0] + " " + strTime;

Upvotes: 0

Furkan Ekinci
Furkan Ekinci

Reputation: 2632

string strDate = "31/05/2013 0:00";
string strTime = "21:22";
strDate = strDate.Replace("0:00", strTime);
DateTime date = Convert.ToDateTime(strDate);

Upvotes: 0

Jahan Zinedine
Jahan Zinedine

Reputation: 14874

As @George said, parse the first value as a DateTime and then another one as TimeSpan and then add the TimeSpan to first parsed value.

Another option is getting the substring of first 10 charachters of first value and concat it with a space with second value and parse it as DateTime.

Upvotes: 1

It'sNotALie.
It'sNotALie.

Reputation: 22794

Say that the first string is called one and the second one is called two, just do this:

DateTime result = DateTime.Parse(one).Date + DateTime.Parse(two).TimeOfDay;

Upvotes: 0

Related Questions