Home > DeveloperSection > Forums > TimeSpan not working
Pawan Shukla
Pawan Shukla

Total Post:29

Points:203
Posted on    November-25-2014 10:04 PM

 ASP.Net Time 
Ratings:


 1 Reply(s)
 903  View(s)
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?



Kamlakar Singh
Kamlakar Singh

Total Post:194

Points:1396
Posted on    November-26-2014 12:14 AM

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


Don't want to miss updates? Please click the below button!

Follow MindStick