如何从TimeSpan变量获取经过的时间到当前时间



我试图坚持我的计时器,我有一个列表中的项目在一个列表视图,所以当一个用户开始任何项目,我想坚持任何项目的计时器。我有一个timespan变量它接受一个整型变量总时间它被转换成分钟

//Get total time and convert to minutes
_TimeSpan = TimeSpan.FromMinutes(TimeSheet.TotalTime);

我如何从_TimeSpan变量中获得经过的时间,就像我如何使用DateTime一样。下面的例子使用了DateTime。现在要获取运行时间:

var startAt = DateTime.Now;
Application.Current.Properties["StartedAt"] = startAt;

Device.StartTimer(TimeSpan.FromSeconds(1), () =>
{
if (cts.IsCancellationRequested)
{
return false;
}
else
{
Device.BeginInvokeOnMainThread(() =>
{
TimeSpan _TimeSpan = DateTime.Now - startAt;

TxtTimer = _TimeSpan.ToString(@"hh:mm:ss");

});
return true;
}
});

您可以使用TimeSpan.Subtract,然后传递已解析的值和当前时间。使用DateTime.Now.TimeOfDay可以得到当前时间为TimeSpan

所以最后的结果应该是:

var difference = DateTime.Now.TimeOfDay.Subtract(_TimeSpan); 

最新更新