多个时间值在c#.net中添加?

时间:2013-01-29 13:28:55

标签: c#

我想在c#.net中添加两个以上的时间值怎么样?例子:

129:43:50 + 20:20:00 + 30:00:10 = 180:04:00

4 个答案:

答案 0 :(得分:2)

也许使用TimeSpan.Add?这确实是一个奇怪的问题。

编辑:

根据您的评论,我建议您查看MSDN上的Standard TimeSpan Format StringsCustom TimeSpan Format Strings,了解如何设置TimeSpan的格式。

答案 1 :(得分:2)

如果您有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);

答案 2 :(得分:0)

方法

怎么样?
public DateTime Add(TimeSpan value)

?这是谷歌的第一个结果。

答案 3 :(得分:0)

您将要使用TimeSpan对象。您需要构建,然后将这些值一起添加。