75

I have a calendar and a textbox that contains a time of day. I want to create a datetime that is the combination of the two. I know I can do it by looking at the hours and mintues and then adding these to the calendar DateTime, but this seems rather messy.

Is there a better way?

7 Answers 7

119

You can use the DateTime.Add() method to add the time to the date.

DateTime date = DateTime.Now;
TimeSpan time = new TimeSpan(36, 0, 0, 0);
DateTime combined = date.Add(time);
Console.WriteLine("{0:dddd}", combined);

You can also create your timespan by parsing a String, if that is what you need to do.

Alternatively, you could look at using other controls. You didn't mention if you are using winforms, wpf or asp.net, but there are various date and time picker controls that support selection of both date and time.

3
  • 1
    after hours i tried for 11:57 pm and this worked: TimeSpan time = new TimeSpan(23, 57, 00); DateTime date_with_time = mydate.Add(time);
    – Shaiju T
    Commented Sep 1, 2015 at 8:58
  • If you're ever in the situation where you have a specific amount of time (eg: X hours or Y minutes) there are helper functions so that you don't have to create a TimeSpan(). eg: DateTime.UtcNow.AddSeconds(10); // a time 10 seconds in the future! Commented Oct 29, 2015 at 19:40
  • @SeanColombo what about current Time that get using ToShortTimeString() ??
    – AminM
    Commented Oct 26, 2017 at 12:05
23

If you are using two DateTime objects, one to store the date the other the time, you could do the following:

var date = new DateTime(2016,6,28);

var time = new DateTime(1,1,1,13,13,13);

var combinedDateTime = date.AddTicks(time.TimeOfDay.Ticks);

An example of this can be found here

2
  • 1
    Just what I was looking for
    – kpkpkp
    Commented Dec 7, 2017 at 21:48
  • 1
    But why not date.Add(time.TimeOfDay) ? Commented Aug 9, 2022 at 11:31
5

Depending on how you format (and validate!) the date entered in the textbox, you can do this:

TimeSpan time;

if (TimeSpan.TryParse(textboxTime.Text, out time))
{
   // calendarDate is the DateTime value of the calendar control
   calendarDate = calendarDate.Add(time);
}
else
{
   // notify user about wrong date format
}

Note that TimeSpan.TryParse expects the string to be in the 'hh:mm' format (optional seconds).

3

Using https://github.com/FluentDateTime/FluentDateTime

DateTime dateTime = DateTime.Now;
DateTime combined = dateTime + 36.Hours();
Console.WriteLine(combined);
3
  • 2
    I love FuentDateTime. If this is the only datetime sum you want to do then it might not be worth it. But, there's a lot of other great stuff in there like finding out when the next Friday is. Commented May 21, 2013 at 10:16
  • @RichardGarside: Do you mean the FluentDateTime NuGet package from the GitHub?
    – J Pollack
    Commented Dec 15, 2015 at 9:00
  • It is available as a Nuget package. There are links on the Github page. Commented Dec 17, 2015 at 11:24
1
   DateTime newDateTime = dtReceived.Value.Date.Add(TimeSpan.Parse(dtReceivedTime.Value.ToShortTimeString()));
1
  • 2
    Although this is is a correct answer, you should provide an explanation to OP instead of providing a code only answer to make sure they (and any other views) understand why the answer is correct..
    – JNYRanger
    Commented Jun 28, 2017 at 17:50
0

Combine both. The Date-Time-Picker does support picking time, too.

You just have to change the Format-Property and maybe the CustomFormat-Property.

0

Old question but figured I'd share my solution, I'm using separate date and time picker controls so I needed to use just the date from one control and just the time from another control which both use . This would work equally as well with time in a textbox though you should probably use a masked textbox or some kind of validation on the text input.

I'm using telerik controls, but you get the idea and it will work the same with any DateTime objects:

var startDateTime = DateTime.Parse($"{dateStart.Value.Date.ToShortDateString()} {tpStart.Value.Value.ToShortTimeString()}");

or if using a textbox for time:

var startDateTime = DateTime.Parse($"{dateStart.Value.Date.ToShortDateString()} {txtTime.Text}");

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.