Don't forget, time and duration are different things. You also need duration. Time is represented by
DataTime
, but duration — more powerful for time arithmetics — by
TimeSpan
. Is my hint already taken?
If not, please keep reading:
DateTime time = DateTime.Now;
int days = 13;
int hours = 12;
int minutes = 55;
int seconds = 12;
time -= new TimeSpan(days, hours, minutes, seconds);
time += new TimeSpan(days, hours);
Now the idea should be completely clear for you. With
TimeSpan
, you can do any reasonable time arithmetic operation you might expect.
Side note: one useful application of such arithmetic operations is measuring time: you can subtract one time from another, returned result is
TimeSpan
:
DateTime before = DateTime.Now;
DateTime after = DateTime.Now;
TimeSpan delay = after - before;
double ms = delay.TotalMilliseconds;
double hours = delay.TotalHours;
—SA