tags:

views:

88

answers:

7

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?

+6  A: 

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

Once you have that, just do:

TimeSpan time = GetTimeFieldData();
dateField = dateField.Add(time);
Reed Copsey
+3  A: 

You can do this quite easily:

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

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

David M
+1  A: 

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

Mark Dykun
+3  A: 

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;
Adam Robinson
I think this one will work the best for me in this case. Thanks!
Mike Wills
A: 

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

J Angwenyi
Not very efficient.
Lazlo
A: 

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.

Franci Penov
A: 

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
}
Seattle Leonard