4imble
4imble

Reputation: 14416

Adding a Time to a DateTime in C#

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?

Upvotes: 75

Views: 219306

Answers (7)

djicode
djicode

Reputation: 1

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}");

Upvotes: 0

sansalk
sansalk

Reputation: 4731

   DateTime newDateTime = dtReceived.Value.Date.Add(TimeSpan.Parse(dtReceivedTime.Value.ToShortTimeString()));

Upvotes: 1

user3258819
user3258819

Reputation: 251

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

Upvotes: 23

Simon
Simon

Reputation: 34830

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

DateTime dateTime = DateTime.Now;
DateTime combined = dateTime + 36.Hours();
Console.WriteLine(combined);

Upvotes: 3

Tom van Enckevort
Tom van Enckevort

Reputation: 4198

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).

Upvotes: 5

Bobby
Bobby

Reputation: 11576

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

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

Upvotes: 0

Simon P Stevens
Simon P Stevens

Reputation: 27499

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.

Upvotes: 119

Related Questions