如何以hr:min:Sec格式添加时间 [英] How To add time in hr:min:Sec format

查看:161
本文介绍了如何以hr:min:Sec格式添加时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我必须节省解决总和所需的时间。如果总和确实错了那么学生必须再做一次。在这段时间我必须将我的时间添加到我保存在数据库中的前一次。例如,在第一次尝试时间为00:15:25(hr:min:sec格式),第二次尝试为00: 10:25。然后在我的数据库中我必须将其保存为00:25:50(添加第一次和第二次尝试时间)



现在我正试图分割时间并单独获取值(即分别取hr,min,sec时间然后单独添加..有没有其他方法..我可以用jquery或javascript ..

In my project i have to save the time taken to solve a sum. If the sum did was wrong then the student have to done it again. In this time i have to add my time with the previous time which i saved in our database..For Example in the first attempt time taken was 00:15:25 (hr:min:sec format),second attempt it is 00:10:25. Then in my database i have to save it as 00:25:50 (adding first and second attempt time)

Right now i''m trying to split the time and getting values separately(ie, taking hr,min,sec time separately and then adding it separately.. Is there any other method.. can i do with jquery or javascript..

推荐答案

最好的方法是从开始时间开始,然后找到结束时间。

The best way is to start with the start time, then find the end time.
DateTime startTime = DateTime.Now;
...
DateTime endTime = DateTime.Now;



如果你再减去它们,你得到一个Timespan:


If you then subtract them, you get a Timespan:

TimeSpan takenSoFar = endTime - startTime;





当谈到第二次尝试时,你会做同样的事情:



When it comes to the second attempt, you do the same thing:

DateTime startTime2 = DateTime.Now;
...
DateTime endTime2 = DateTime.Now;



如果你再减去它们,你得到一个Timespan:


If you then subtract them, you get a Timespan:

TimeSpan takenThisAttempt = endTime2 - startTime2;





然后你可以将两个Timespans一起添加:



You can then add the two Timespans together:

takenSoFar += takenThisAttempt;


You can use TimeSpan

TimeSpan t = TimeSpan.FromSeconds(seconds);

and

use t.Hours, t.Minutes and t.Seconds to format the string how ever you want.


您好,



使用以下代码。



Hi,

Use the following code.

TimeSpan time1 = new TimeSpan(0, 15, 25);
            TimeSpan time2 = new TimeSpan(0, 25, 50);
            TimeSpan time = time1.Add(time2);


这篇关于如何以hr:min:Sec格式添加时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆