私は、試験の残り時間を示すカウントダウンタイマーを配置しました。TimeSpanとDateTimeはどのように差し引かれますか?
私はユーザーが試験を完了するのに要した合計時間を計算しようとしています。
これを計算するには、残りの時間がカウントダウンタイマーで表示されているが、出力に現在の日付が表示されています。-09:04:05.1564371
...ここではコードを実行した時刻です。
私は間違いをどこにしているのか分かりません。私のコードを見て、私が間違いを犯している場所とその解決策を教えてください。
Examdemo.aspx.cs: -
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: -
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'
}
}
タイマーの設定は何ですか?セッションを使用することは賢明ですか?私はこのデータをデータベースに配置します。 –
論理的には、開始時間を知っているか、または:許容される最大持続時間を知っていなくても、かかる時間を知ることはできません。セッション変数に 'start_time'を格納してください(またはJeroenが提案するように、データベース)、あなたの所要時間は'(DateTime.Now - startTime) 'になります。あなたのコードは、現在、あなたに '(timeLeft - (currentTime - lastMidnight))' – Rob