本文介绍了如何显示从特定时间减去的剩余时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想显示铃声系统的剩余时间。
当我们处于上午8:00到8:45的第一个时段时,我想在第一个时段结束时显示剩余时间。如上所述,当我们处于上午8:50 - 9:35的第二个时期时,我想展示同样的东西。
我用的是一个显示剩余分钟和秒的计时器。以下是我到目前为止:
I want to display the remaining time from periods for the bell system.
When we are in the first period which is 8:00 to 8:45 am, I want to display remaining time to the end of the first period. So like above, when we are in the second period which is 8:50 - 9:35am, I want to display the samething.
What I used was a timer to display remaining minutes and seconds. Here is what I have so far:
private void timerPeriods_Tick(object sender, EventArgs e)
{
DateTime endTime= new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Hour, 8, 45, 0);
TimeSpan timeRemaining = TimeSpan.FromTicks(DateTime.Now.Subtract(endTime).Ticks);
lblStatus.Text = startTime.ToString();
}
然而我无法让它发挥作用。有更好的方法吗?
However I couldn't make it work. Is there a better way to do it?
推荐答案
// Compute start times
List<datetime> startTimes = new List<datetime>();
DateTime startTime = DateTime.Today.AddHours (8);
int periodLengthMins = 45;
int breakLengthMins = 5;
for (int i=0; (i < 10); i++) {
startTimes.Add (startTime);
startTime = startTime.AddMinutes (periodLengthMins + breakLengthMins);
}
在您的计时器刻度处理程序中执行此操作:
Do this in your timer tick handler:
// Determine time to start of next period
DateTime now = DateTime.Now;
DateTime? nextPeriod = startTimes.FirstOrDefault (p => (p - now).Ticks >= 0);
lblStatus.Text = nextPeriod.HasValue ?
(nextPeriod - now).ToString() : "No next period";
/ ravi
这篇关于如何显示从特定时间减去的剩余时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!