你如何计算C#中的累计时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/131812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you calculate accumulative time in C#?
提问by
I want to calculate the time span between 2 times which I saved in a database. So literally I want to know the length of time between the 2 values.
我想计算我保存在数据库中的两次之间的时间跨度。所以从字面上看,我想知道这两个值之间的时间长度。
14:10:20 - 10:05:15 = 02:05:05
14:10:20 - 10:05:15 = 02:05:05
So the result would be 02:05:05.
所以结果将是 02:05:05。
How would I be able to achieve this using C#?
我如何才能使用 C# 实现这一目标?
14:10:20 is the format I saved it in in my database.
14:10:20 是我在数据库中保存的格式。
采纳答案by cruizer
Read the two time values into TimeSpan variables, then perform a .Subtract()
on the bigger TimeSpan variable to get the TimeSpan result.
将两个时间值读入 TimeSpan 变量,然后.Subtract()
对较大的 TimeSpan 变量执行 a以获得 TimeSpan 结果。
E.g. TimeSpan difference = t1.Subtract(t2);
.
例如TimeSpan difference = t1.Subtract(t2);
。
回答by Niklas Winde
DateTime objects support the "-" operator so you can just read your times into such objects and subtract them. To test, check this out:
DateTime 对象支持“-”运算符,因此您可以将时间读入此类对象并减去它们。要测试,请查看:
DateTime then = DateTime.Now;
Thread.Sleep(500);
DateTime now = DateTime.Now;
TimeSpan time = now - then;
MessageBox.Show(time.ToString());
回答by lowglider
Your first step will be to get the timevalues stored in your database into .NET DateTime
structs.
您的第一步是将存储在数据库中的时间值转换为 .NETDateTime
结构。
If you stored them as SQL-DateTime values in the database you can get them directly as DateTime
. It would look something like this:
如果您将它们作为 SQL-DateTime 值存储在数据库中,您可以直接将它们作为DateTime
. 它看起来像这样:
SQLCommand getTimeCommand = new SQLCommand("SELECT time FROM table", dbConnection);
SQLDataReader myReader = getTimeCommand.ExecuteReader();
while (myReader.Read())
{
DateTime time = myReader.GetDateTime(0);
}
myReader.Close();
Your implementation might differ, refer to the ADO.NET documentation in the MSDN library.
您的实现可能会有所不同,请参阅 MSDN 库中的 ADO.NET 文档。
If you already got a string representing the time you can parse the string into a DateTime
using the static methods
如果您已经有一个表示时间的字符串,您可以DateTime
使用静态方法将字符串解析为 a
DateTime.Parse
or
或者
DateTime.ParseExact
In your case you might need to use ParseExact
, which can pe provided with a format-string defining how to read the string. Examples should be found in the MSDN library.
在您的情况下,您可能需要使用ParseExact
,它可以提供一个定义如何读取字符串的格式字符串。示例应该可以在 MSDN 库中找到。
Durations in .NET are stored inside a TimeSpan
struct. Getting the elapsed time between to datetimes is easy:
.NET 中的持续时间存储在TimeSpan
结构中。获取日期时间之间经过的时间很容易:
DateTime time1, time2; //filled with your timevalues from the db
TimeSpan elapsed = d2 - d1;
elapsed
now contains the timespan between the two DateTimes
. There are several members for the struct to access the TimeSpan
. Look into the MSDN-Library to find the ones you need.
elapsed
现在包含两者之间的时间跨度DateTimes
。结构有几个成员可以访问TimeSpan
. 查看 MSDN-Library 以找到您需要的那些。