简体   繁体   English

时间跨度不起作用

[英]TimeSpan not working

Heres the code I have re arranged, but still have same problem, not able to find my start and end times on last page? 这是我重新排列的代码,但是仍然有相同的问题,无法在最后一页找到我的开始时间和结束时间? How do I do this? 我该怎么做呢?

public void start()
    {
        DateTime startTime = DateTime.Now;
    }

    protected void btnStart_Click(object sender, EventArgs e)
    {
        start();
        Response.Redirect("~/end.aspx");
    }

public void end()
    {
        DateTime endTime = DateTime.Now;
    }
    protected void btnEnd_Click(object sender, EventArgs e)
    {
        end();
        Response.Redirect("~/display.aspx");
    }

public partial class display : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        TimeSpan timeSpent = endTime - startTime;

        lblDisplay.Text = string.Format("Time: {0}", timeSpent);
    }
}

Now can anyone help me on this? 现在有人可以帮我吗?

You can use 您可以使用

DateTime start = DateTime.Now;
//........    some code here.......
DateTime end = DateTime.Now;
TimeSpan timeSpent = end - start;

and then use 然后使用

timeSpent.TotalMilliseconds or timeSpent.TotalSeconds .....

the properties of the timespan are all in the intellisense (minutes/days/hours/seconds/milliseconds....) 时间跨度的所有属性都在智能感知中(分钟/天/小时/秒/秒/毫秒...。)

您可以尝试以下方法:

TimeSpan timeSpent = end.Subtract(start);

You do not need to convert this to a string. 您无需将其转换为字符串。

DateTime start = DateTime.Now;
DateTime end = DateTime.Now;

(Note: Those two times above will be identical) (注意:以上两次相同)

Once you have done that, you can use one of the other techniques shown above to get the timespan: 完成此操作后,可以使用上面显示的其他技术之一来获取时间跨度:

var timeSpent = (end - start);

or 要么

TimeSpan timeSpent = end.Subtract(start);

To display it: 要显示它:

Console.WriteLine(timeSpent.TotalMilliseconds);

Now, go code! 现在,去编码! :) :)

 TimeSpan span = end.Subtract ( start );

这将为您提供从开始到结束的时间

如果您想知道工作时间,可以使用Stopwatch类http://msdn.microsoft.com/zh-cn/library/system.diagnostics.stopwatch(v=vs.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM