Reputation:
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
Reputation: 3099
TimeSpan a = new TimeSpan(2,0,0);
TimeSpan b = new TimeSpan(3,0,0);
TimeSpan c = a.Add(b);
Upvotes: -3
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
Reputation: 99694
With TimeSpan
TimeSpan a = new TimeSpan(2, 0, 0);
TimeSpan b = new TimeSpan(3, 0, 0);
TimeSpan c = a + b;
Upvotes: 5
Reputation: 15253
Try this:
http://msdn.microsoft.com/en-us/library/system.datetime.addhours.aspx
Upvotes: 0