TIMESPAN NOT WORKING

Pawan Shukla

Total Post:29

Points:203
Posted by  Pawan Shukla
Time 
 1113  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. Kamlakar Singh

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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!