Question

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?

Était-ce utile?

La solution 2

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

Autres conseils

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....)

You could try this:

TimeSpan timeSpent = end.Subtract(start);
 TimeSpan span = end.Subtract ( start );

This will get you the time that elapased between the start and the end

If you want to know working time, you could use Stopwatch class http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(v=vs.110).aspx

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top