Join Date and Time to DateTime in C#

C#Datetime

C# Problem Overview


I am retrieving data from an iSeries where there is a separate date and time fields. I want to join them into a DateTime field in my C# project. I don't see a way to add just a time to a DateTime field. How would you suggest accomplishing this?

C# Solutions


Solution 1 - C#

You can do this quite easily:

DateTime dateOnly;
DateTime timeOnly;
...
DateTime combined = dateOnly.Date.Add(timeOnly.TimeOfDay);

TimeOfDay returns a TimeSpan, which you then add to the date.

Edit (thanks to commenters below) - to be safe, use dateOnly.Date to ensure the date part only.

Solution 2 - C#

How are they being stored? Assuming that the date portion is being stored as a DateTime of midnight of the day in question and the time is a TimeSpan, you can just add them.

DateTime date = ...;
TimeSpan time = ...;

DateTime result = date + time;

Solution 3 - C#

You could easily construct a TimeSpan from your "time" field.

Once you have that, just do:

TimeSpan time = GetTimeFieldData();
dateField = dateField.Add(time);

Solution 4 - C#

Datetime date = new DateTime(Date1.Year, Date1.Month, Date1.Day, Time1.Hour, Time1.Minute, Time1.Second); 

Solution 5 - C#

You can add a TimeSpan to a DateTime and write something like this.

// inside consuming function
ISeriesObject obj = getMyObject();
DateTime dt = getDate(obj) + getTime(obj);

private DateTime getDate(ISeriesObject obj)
{
     //return a DateTime
}


private TimeSpan getTime(ISeriesObject obj)
{
     //return a TimeSpan
}

Solution 6 - C#

This should do:

var output = date.Date + time.TimeOfDay;

or

var output = new DateTime(date.Year, date.Month, date.Day,
                          time.Hour, time.Minute, time.Second);

suppose that both variable date and time are both of Type DateTime

Solution 7 - C#

My answer addresses joining two objects of DateOnly and TimeOnly in .NET 6:

DateOnly orderDate = ...
TimeOnly orderTime = ...
DateTime orderDateTime = orderDate.ToDateTime(orderTime);

Solution 8 - C#

Note that adding the time to the date is not your biggest problem here. As @Reed Copsey mentioned, you just create a DateTime from the date and then .Add the time.

However, you need to make sure that the iSeries date and time (a Unix time most probably) are in the same representation as the .Net representation. Thus, you most probably need to convert it by adding it to a Jan 1, 1970 DateTime as well.

Solution 9 - C#

Cant you simply format the date part and time part as separate strings, then join them together? Then you can parse the string back to a DateTime object

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMike WillsView Question on Stackoverflow
Solution 1 - C#David MView Answer on Stackoverflow
Solution 2 - C#Adam RobinsonView Answer on Stackoverflow
Solution 3 - C#Reed CopseyView Answer on Stackoverflow
Solution 4 - C#Mark DykunView Answer on Stackoverflow
Solution 5 - C#Seattle LeonardView Answer on Stackoverflow
Solution 6 - C#NerdroidView Answer on Stackoverflow
Solution 7 - C#Shimmy WeitzhandlerView Answer on Stackoverflow
Solution 8 - C#Franci PenovView Answer on Stackoverflow
Solution 9 - C#Julius AView Answer on Stackoverflow