Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have placed a countdown timer which shows remaining time to complete the exam.

I am trying to calculate the total time taken by the user to complete the exam.

To calculate this, I subtracted the end_time of the exam with the remaining time shows in the countdown timer but I am getting current date in the output .For example -09:04:05.1564371...here it is the time when I ran the code.

I don't know where I am making mistake. Have a look at my code show me where I am making mistake and what is the solution.

What I have tried:

Examdemo.aspx.cs :-

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;


public partial class Student_Examdemo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


       if (!Page.IsPostBack || Session["end_t"] == null)
         {
             DateTime start_time = DateTime.Now;
             DateTime end_time = start_time.AddMinutes(3);
             Session["end_t"] = end_time;
         } 
    }

    protected void timer1_tick(object sender, EventArgs e)
    {
          DateTime dt = (DateTime)Session["end_t"];
         DateTime dt_curr = DateTime.Now;
         TimeSpan ts = dt - dt_curr;
         lblTimer.Text = ts.Hours.ToString() + ":" + ts.Minutes.ToString() + ":" + ts.Seconds.ToString();

         if (ts.Minutes == 0)
         {
             timer1.Enabled = false;
             Response.Redirect("/Student/Result.aspx");
         }

        TimeSpan usedTime = ts.Subtract(dt.TimeOfDay); //calculating the total time taken by user
        Session["takentime"] = usedTime.ToString(); //storing the calculated time in the session
    }

            protected void btn_Click(object sender, EventArgs e)
          {
                  //statements

        Response.Redirect("/Student/Result.aspx?Score=" + Label2.Text +"&AttemptedQues=" +Label3.Text+ "&CorrectAns=" +Label4.Text);

    }

    }



Result.aspx.cs :-

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Student_Result : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Request.QueryString["Score"];
        Label2.Text = Request.QueryString["AttemptedQues"];
        Label3.Text = Request.QueryString["CorrectAns"];
        Label5.Text = Session["takentime"] as string;  //displaying the calculated time...got output like this : '-09:04:05.1564371'
    }
}
Posted
Updated 3-May-16 19:26pm

Store the Start Time in a session as

C#
if (!Page.IsPostBack || Session["end_t"] == null)
        {
            DateTime start_time = DateTime.Now;
         Session["start_t"] = start_time;
            DateTime end_time = start_time.AddMinutes(3);
            Session["end_t"] = end_time;
        }


and Find the difference in the timer event

C#
DateTime dtstart = (DateTime)Session["start_t"];
        TimeSpan timeUsed = DateTime.Now - dtstart;
        Session["takentime"] = timeUsed.ToString();
 
Share this answer
 
Comments
BillWoodruff 4-May-16 1:31am    
The OP does save the initial DateTime + 3 minutes in the 'end_time' variable, and in the 'end_t' Session variable. The overall logic appears correct with the exception I note in my post, here.
Karthik_Mahalingam 4-May-16 1:39am    
We don't know his logic behind 'adding 3 mins'. Will wait for his reply to get clarified.
BillWoodruff 4-May-16 1:49am    
imho the logic for adding 3 minutes is easy to understand: the OP wants the person taking the test to be able to start taking it. I suggest 3 minutes is just a "trial" setting for testing.

But, you are very right to suggest waiting for the OP to clarify .. very dangerous here to assume anything about the code and questions we see, here :)
Karthik_Mahalingam 4-May-16 2:40am    
Bill, my answer accepted :-)
Member 12170781 4-May-16 2:19am    
@KARTHIK Bangalore after applying your suggestion I am getting exact output but the format of output is like this "00:00:20.9214091"..here 20 is the seconds and 9214091 is the milliseconds, am I right ?. I don't want to show the milliseconds..how do I do that?
Specific comments:

1. what is this about:
C#
TimeSpan usedTime = ts.Subtract(dt.TimeOfDay);
You know the time used from your previous calculation of the difference between end_time and DateTime.Now

2. why don't you calculate the time-taken before the Timer loop is terminated ?

This is where learning to debug is very important: set break-points (or use "Alert" messages) in your code, and check if the end-time DateTime you saved is really being used in the Timer 'Click event ... or, is that initial value somehow being ignored.

I note you define a start_time variable and assign the current time to it, but, then, never use it, never save it to the session ... why is that ?

Is there a possibility you are getting DateTime values for two different time-zones ... I don't think so, but I suggest you want to eliminate that as a possible cause.
 
Share this answer
 
v2
First use DateTime.Subtract Method[^] to get the different as a TimeSpan object
Then get the desired unit of time using the appropriate TimeSpan property such as TimeSpan.TotalSeconds[^].
 
Share this answer
 
v2
Comments
BillWoodruff 4-May-16 1:18am    
Hi Peter, afasik using the minus operator on DateTime invokes the same code as using DateTime.Subtract, and both ignore possible time-zone context differences.

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