tags:

views:

114

answers:

5

I want to add time values, e.g. 2:00 + 3:00 = 5:00.

How can I do that in ASP.NET using C#?

+2  A: 

Have a look at TimeSpan.

Mark Redman
A: 
TimeSpan a = new TimeSpan(2,0,0);
TimeSpan b = new TimeSpan(3,0,0);
TimeSpan c = a.Add(b);
Boris Modylevsky
Way to copy somebody's answer. AND you did not even format it! That is pointless.
Josh Stodola
Josh, thanks for pointing this out. I much appreciate your comment!
Boris Modylevsky
+5  A: 

With TimeSpan

TimeSpan a = new TimeSpan(2, 0, 0);
TimeSpan b = new TimeSpan(3, 0, 0);
TimeSpan c = a + b;
Bob
Just thought it might be worth adding that there is nothing special here for ASP.NET, this is just standard C#
Bob
+1  A: 

I would start by looking at the DateTime and TimeSpan structures. Using these types you can pretty easily perform most Date/Time operations.

Specifically look at these methods:

A simple example would be:

public TimeSpan? AddTime(string time1, string time2)
{
    TimeSpan ts1;
    TimeSpan ts2;

    if (TimeSpan.TryParse(time1, out ts1) &&
        TimeSpan.TryParse(time2, out ts2))
    {
        return ts1.Add(ts2);
    }

    return null;
}
akmad