user190618
user190618

Reputation:

How can I add time values in ASP.NET?

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

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

Upvotes: 0

Views: 3342

Answers (5)

Boris Modylevsky
Boris Modylevsky

Reputation: 3099

TimeSpan a = new TimeSpan(2,0,0);
TimeSpan b = new TimeSpan(3,0,0);
TimeSpan c = a.Add(b);

Upvotes: -3

akmad
akmad

Reputation: 19751

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;
}

Upvotes: 1

Bob
Bob

Reputation: 99694

With TimeSpan

TimeSpan a = new TimeSpan(2, 0, 0);
TimeSpan b = new TimeSpan(3, 0, 0);
TimeSpan c = a + b;

Upvotes: 5

Mark Redman
Mark Redman

Reputation: 24515

Have a look at TimeSpan.

Upvotes: 2

Related Questions