I want to add time values, e.g. 2:00 + 3:00 = 5:00.
How can I do that in ASP.NET using C#?
I want to add time values, e.g. 2:00 + 3:00 = 5:00.
How can I do that in ASP.NET using C#?
TimeSpan a = new TimeSpan(2,0,0);
TimeSpan b = new TimeSpan(3,0,0);
TimeSpan c = a.Add(b);
With TimeSpan
TimeSpan a = new TimeSpan(2, 0, 0);
TimeSpan b = new TimeSpan(3, 0, 0);
TimeSpan c = a + b;
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;
}