如何在C#web app中获取持续时间 [英] How to get the time duration in C# web app

查看:82
本文介绍了如何在C#web app中获取持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有两个图像按钮,当点击第一个按钮时应该开始时间,当按下第二个按钮时,时间应该停止并计算持续时间和显示所有开始,结束时间和持续时间等信息(按下第二个按钮后应显示)



提前谢谢。

Hi,
I have two image buttons,when the first button is clicked the time should start and when the second button is pressed the time should stop and calculate the duration of time and diaplay all the info like start,end time and duration(it should display once the second button is pressed)

Thank you in advance.

推荐答案

此代码持续请求之间的开始时间



This code persists the start time between requests

<asp:Button ID="btnStart" runat="server" Text="Start" OnClick="btnStart_Click" />
<asp:Button ID="btnStop" runat="server" Text="Stop" OnClick="btnStop_Click" />

<p>
    Started: <asp:Literal ID="LiteralStart" runat="server" /><br />
    Stopped: <asp:Literal ID="LiteralStop" runat="server" /><br />
    Duration: <asp:Literal ID="LiteralDuration" runat="server" /><br />
</p>







protected void btnStart_Click(object sender, EventArgs e)
{
    DateTime dt = DateTime.Now;
    LiteralStart.Text = dt.ToString();

    ViewState["startTime"] = dt;
}

protected void btnStop_Click(object sender, EventArgs e)
{
    DateTime dt = DateTime.Now;
    LiteralStop.Text = dt.ToString();

    DateTime startTime = (DateTime)ViewState["startTime"];

    TimeSpan duration = dt - startTime;

    LiteralDuration.Text = duration.TotalSeconds.ToString();
}


因此设置两个按钮Click事件处理程序和一个Form级私有DateTime变量。

一个计时器,将变量设置为当前日期和时间,另一方面,从当前日期和时间中减去它。然后你有一个可以显示的TimeSpan:

So set up two button Click event handlers, and a Form level private DateTime variable.
On one timer, set the variable to the current date and time, and on the other, subtract it from the current date and time. You then have a TimeSpan you can display:
private DateTime startTime = DateTime.Now;
void butStart_Click(Object sender, EventArgs e)
   {
   startTime = DateTime.Now;
   }
void butShow_Click(Object sender, EventArgs e)
   {
   TimeSpan diff = DateTime.Now - startTime;
   labDifference.Text = diff.ToString();
   }


这篇关于如何在C#web app中获取持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆