我已经安排了一个倒数计时器,显示完成考试的剩余时间。
我正在尝试计算用户完成考试所用的总时间。
为了计算这一点,我用倒计时器中的剩余时间显示减去考试的end_time
,但我在输出中得到当前日期。例如-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'
}
}
答案 0 :(得分:1)
以下行是错误的......在您的代码中。
TimeSpan usedTime = ts.Subtract(dt.TimeOfDay); //calculating the total time taken by user
ts
已经包含了00:02:59.9919920
...
然后用end_time.TimeOfDay
减去04:20:53.7564800
后的from ldap_dir.ldap_query.Lib import ldap
所以差异是错误的,没有意义。
答案 1 :(得分:1)
如果您需要的只是以毫秒为单位的时间,那么您不需要这么做。
(dtend - dtstart).TotalMilliseconds
答案 2 :(得分:0)
我以某种方式找出了问题,这是解决方案:
将开始时间存储在会话中
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;
}
并找出计时器事件
的区别DateTime dtstart = (DateTime)Session["start_t"];
TimeSpan timeUsed = DateTime.Now - dtstart;
TimeSpan removeMillSeconds = new TimeSpan(timeUsed.Days, timeUsed.Hours, timeUsed.Minutes, timeUsed.Seconds, 0);
Session["takentime"] = removeMillSeconds.ToString();