TimeSpan not working

Total Post:29

Points:203
Time 
 1430  View(s)
Ratings:
Rate this:

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?

  1. Post:194

    Points:1396
    Re: TimeSpan not working

    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! :)

      Modified On Apr-07-2018 12:58:25 AM

Answer