如何计算在asp.net中使用timespan完成在线考试所需的时间 [英] how to calculate time taken to complete the online exam using timespan in asp.net
问题描述
我已经抓住了在page_load事件中开始考试的时间。
i have captured the time when examination was started in page_load event.
DateTime ExmStartTym = DateTime.Now();
考试结束时我想计算考生的时间。
这里我正在使用TimeSpan Class对象来做这个..
请帮我这样做...
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)
{
}
}
推荐答案
我完成了c #Windows表单应用程序我希望这对你有所帮助
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();
如果你想在几分钟或几小时内有不同的方法,如totalminutes等
If you want in minutes or hours there is different methods like totalminutes etc
简单:
Simple:
Timespan diff = dateTimeEnd - dateTimeStart;
string timeTaken = string.Format("{0} hours and {1} minutes", diff.Hours + diff.Days * 24, diff.Minutes);
在考试开始时获取开始时间:
例如:
Get the Start time at beginning of exam:
eg:
TimeSpan examStartTime = DateTime.Now.TimeOfDay();
当结束按钮是点击,再次获取当前时间:
例如:
When the End button is clicked, get the current time again:
eg:
TimeSpan examEndTime = DateTime.Now.TimeOfDay();
然后使用TimeSpan通过减去结束时间的开始时间来计算差异:
例如:
Then use TimeSpan to work out the difference by substracting the start time from the end time:
eg:
TimeSpan duration = examEndTime.Subtract(examStartTime);
这篇关于如何计算在asp.net中使用timespan完成在线考试所需的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!