我如何返回时间跨度值? [英] how can i return time span value?
本文介绍了我如何返回时间跨度值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将总时间返回为时间跨度类型?
还是我的solutin就在这里?
如果您认为此代码有误,请告诉我?
请看一下这段代码,尝试解决我的问题.
活动是一个列表,并且timelenght等于endtime-starttime
我想在这里将所有timeleght都添加为总时间,但我不知道如何返回总时间?
请帮我吗?
但在任何地方谢谢你
how can i return totaltime as a timespan type''s?
or my solutin is right here?
and if you think this code have a mistake tell me ?
look at this code please and try to solve my problem .
activity is a list and timelenght equals endtime - starttime
i want to adding all of timeleght''s here as totaltime and i dont know how can i return totaltime?
plaease help me?
but anywhere thank you
public TimeSpan calculateTotaltime()
{
foreach (Activity a in myData.activities)
{
a.totaltime += a.timelenght;
}
return ? ;
}
请帮忙
推荐答案
DateTime then = DateTime.Now;
Thread.Sleep(1000);
TimeSpan span = DateTime.Now - then;
我不知道您到底是什么Activity
类型,所以我不知道timelength
的类型.根据您的单位,可以创建System.TimeSpan
的实例.例如,使用静态方法System.TimeSpan.FromMilliseconds
.
还有FromTicks
,FromSeconds
,FromMinutes
等方法.
请参阅 http://msdn.microsoft.com/en-us/library/system.timespan.aspx [^ ] .
—SA
I don''t know exactly whatActivity
type do you mean, so I don''t know the type of thetimelength
. Depending on the units of you can create an instance ofSystem.TimeSpan
. For example, use static methodSystem.TimeSpan.FromMilliseconds
.
There are also the methodsFromTicks
,FromSeconds
,FromMinutes
, etc.
See http://msdn.microsoft.com/en-us/library/system.timespan.aspx[^].
—SA
如果我正确理解了您的问题,您需要做的是将所有活动的持续时间加起来,以得出如下总数:
If I understood your question correctly, what you need to do is sum all of activities'' durations to get the total like this:
public TimeSpan calculateTotaltime()
{
TimeSpan total = TimeSpan.Zero;
foreach (Activity a in myData.activities)
total += a.timelength;
return total;
}
total
是列表中所有活动的持续时间之和.
Here total
is the sum of the durations of all the activities in your list.
这篇关于我如何返回时间跨度值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文