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?
You could easily construct a TimeSpan from your "time" field.
Once you have that, just do:
TimeSpan time = GetTimeFieldData();
dateField = dateField.Add(time);
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.
Datetime date = new DateTime(Date1.Year, Date1.Month, Date1.Day, Time1.Hour, Time1.Minute, Time1.Second);
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;
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
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.
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
}