我想在 c#.net 中添加两个以上的时间值如何?例子:
129:43:50 + 20:20:00 + 30:00:10 = 180:04:00
Maybe using TimeSpan.Add
? This really is an odd question though.
EDIT:
Based on your comment I suggest you take a look at Standard TimeSpan Format Strings and Custom TimeSpan Format Strings on MSDN to see how you can format the TimeSpan
.
如果你有List<TimeSpan>
你可以使用 LINQ:
List<TimeSpan> times = new List<TimeSpan>();
TimeSpan total = times.Aggregate(new TimeSpan(), (t1, t2) => t1 + t2);
然后,对于格式化使用:
string result = string.Format ("{0:00}:{1:00}:{2:00}", (int)total .TotalHours, total.Minutes, total.Seconds);
How about the method
public DateTime Add(TimeSpan value)
? Its fairly the first result on google.
您将要使用TimeSpan对象。您需要构造,然后将这些值相加。