Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have captured the time when examination was started in page_load event.

DateTime ExmStartTym = DateTime.Now();


when exam ends i want to calculate the time the examinee has taken.
here i am using TimeSpan Class object to do this..
please help me to do this...

protected void btnend_Click(object sender, EventArgs e)
   {
       try
       {
           UpdateAnswers();
           TimeSpan timeTkn =
           Response.Redirect("~/examend.aspx?totQn='"+lblTotQns.Text+"'&timeTkn='"++"'");
       }
       catch (Exception)
       {

       }
   }
Posted

i done it c# Windows form application i hope this will helpful for you

/* When click on start button */
DateTime from = DateTime.Now;

/* when click on end button */
DateTime to = DateTime.Now;

/* total time */
TimeSpan total = to - from ;

string totalime = to.ToString("t");

/* In seconds  */
string totaltime = total.TotalSeconds.ToString();


If you want in minutes or hours there is different methods like totalminutes etc
 
Share this answer
 
v2
Comments
[no name] 30-Nov-13 5:58am    
thanks. Finally i did this and your ideas helped me alot. i used below code to do this in my asp.net apps.
DateTime exmStTym = Convert.ToDateTime(Session["ExmStartTym"]);
DateTime exmEndTym = DateTime.Now;
TimeSpan timeTkn = exmEndTym.Subtract(exmStTym);
[no name] 1-Dec-13 2:42am    
good friend ,thanks for reply
Simple:
C#
Timespan diff = dateTimeEnd - dateTimeStart;
string timeTaken = string.Format("{0} hours and {1} minutes", diff.Hours + diff.Days * 24, diff.Minutes);
 
Share this answer
 
Get the Start time at beginning of exam:

eg:
C#
TimeSpan examStartTime = DateTime.Now.TimeOfDay();


When the End button is clicked, get the current time again:

eg:
C#
TimeSpan examEndTime = DateTime.Now.TimeOfDay();



Then use TimeSpan to work out the difference by substracting the start time from the end time:

eg:
C#
TimeSpan duration = examEndTime.Subtract(examStartTime);
 
Share this answer
 
v2
Comments
[no name] 30-Nov-13 2:38am    
i tried the above code
Datetime examStartTime = DateTime.Now.TimeOfDay(); it gives compile time error "cannot convert Timespan to Datetime". then i modified the code for explicit conversion
ExmStartTym = Convert.ToDateTime(DateTime.Now.TimeOfDay); then it throws exception.
Unable to cast object of type 'System.TimeSpan' to type 'System.IConvertible'.
Nick Ginis 30-Nov-13 5:58am    
My fault. TimeOfDay() returns a TimeSpan so just declare it as one instead of DateTime. Will edit answer to reflect that.
[no name] 30-Nov-13 6:01am    
thanks. Finally i did this and your ideas helped me alot. i used below code to do this in my asp.net apps.
DateTime exmStTym = Convert.ToDateTime(Session["ExmStartTym"]);
DateTime exmEndTym = DateTime.Now;
TimeSpan timeTkn = exmEndTym.Subtract(exmStTym);

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