Click here to Skip to main content
16,016,678 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I just tool over a testing website that was writen in c#. I m new to C# and need some help. the code is for the time it took for the test to be completed, it works to a point the main issue is it only does the sec even if you spent 30 mins on the test. I need it to do hours mins and seconds.

C#
((Label)e.Row.FindControl("LabelTimeTaken")).Text = ((int)DataBinder.Eval(e.Row.DataItem, "SecondsTaken") / 60).ToString() + " mins " + ((int)DataBinder.Eval(e.Row.DataItem, "SecondsTaken") % 60) + " secs";


any help would be great.
Posted
Updated 28-Nov-11 9:30am
v2

1 solution

No, you don't need hours, minutes and seconds. You need the structure System.TimeSpan and timing using System.Diagnostics.Stopwatch, which is the most accurate tool you can get. See the sample on this page:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[^].

It shows you how to get TimeSpan, and the unit of measurements are irrelevant here, you can use any one, in fractional values (double) or combination of units; this is up to you. And the accuracy is way better than that you expected. You can examine it using static properties System.Diagnostics.Stopwatch.Frequency and System.Diagnostics.Stopwatch.IsHighResolution. Looking at your code, I expect you to be surprised. :-)

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900