C# 计算两次之间有多少分钟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9017498/
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
Calculating how many minutes there are between two times
提问by PJW
I have a datagridview in my application which holds start and finish times. I want to calculate the number of minutes between these two times. So far I have got:
我的应用程序中有一个 datagridview,用于保存开始和结束时间。我想计算这两次之间的分钟数。到目前为止,我有:
var varFinish = tsTable.Rows[intCellRow]["Finish Time"];
TimeSpan varTime = (DateTime)varFinish - (DateTime)varValue;
int intMinutes = TimeSpan.FromMinutes(varTime);
But the last line won't compile because it says I am using invalid arguments for the Timespan constructor. I've researched quite a bit about how to calculate the number of minutes between two times, but I'm hitting a bit of a brick wall. Can someone please advise me on the best way to achieve my objective.
但是最后一行不会编译,因为它说我对 Timespan 构造函数使用了无效参数。我已经研究了很多关于如何计算两次之间的分钟数,但我碰到了一些砖墙。有人可以就实现目标的最佳方式向我提出建议。
EDIT/
编辑/
Now my code is as follows:
现在我的代码如下:
var varFinish = tsTable.Rows[intCellRow]["Finish Time"];
TimeSpan varTime = (DateTime)varFinish - (DateTime)varValue;
int intMinutes = (int)varTime.TotalMinutes;
But I am getting an invalid cast on the second line. Both varFinish and varValue are times e.g. 10:00 and 8:00 say. So not sure why they won't cast to type DateTime?
但是我在第二行得到了一个无效的演员。varFinish 和 varValue 都是时间,例如 10:00 和 8:00。所以不确定为什么他们不会强制转换为 DateTime 类型?
采纳答案by Kane
Try this
尝试这个
DateTime startTime = varValue
DateTime endTime = varTime
TimeSpan span = endTime.Subtract ( startTime );
Console.WriteLine( "Time Difference (minutes): " + span.TotalMinutes );
Edit: If are you trying 'span.Minutes', this will return only the minutes of timespan [0~59], to return sum of all minutes from this interval, just use 'span.TotalMinutes'.
编辑:如果您尝试使用“span.Minutes”,这将仅返回时间跨度 [0~59] 的分钟数,要返回此间隔中所有分钟数的总和,只需使用“span.TotalMinutes”。
回答by Dr. ABT
In your quesion code you are using TimeSpan.FromMinutesincorrectly. Please see the MSDN Documentationfor TimeSpan.FromMinutes, which gives the following method signature:
在您的问题代码中,您使用TimeSpan.FromMinutes不正确。请参阅TimeSpan.FromMinutes的MSDN 文档,其中提供了以下方法签名:
public static TimeSpan FromMinutes(double value)
hence, the following code won't compile
因此,以下代码将无法编译
var intMinutes = TimeSpan.FromMinutes(varTime); // won't compile
Instead, you can use the TimeSpan.TotalMinutesproperty to perform this arithmetic. For instance:
相反,您可以使用TimeSpan.TotalMinutes属性来执行此算术。例如:
TimeSpan varTime = (DateTime)varFinish - (DateTime)varValue;
double fractionalMinutes = varTime.TotalMinutes;
int wholeMinutes = (int)fractionalMinutes;
回答by Tim Schmelter
double minutes = varTime.TotalMinutes;
int minutesRounded = (int)Math.Round(varTime.TotalMinutes);
TimeSpan.TotalMinutes: The total number of minutes represented by this instance.
TimeSpan.TotalMinutes:此实例表示的总分钟数。
回答by Alexander R
You just need to query the TotalMinutesproperty like this varTime.TotalMinutes
你只需要TotalMinutes像这样查询属性varTime.TotalMinutes
回答by Asif Mehmood
If the difference between endTime and startTime is greater than or equal to 60 Minutes, the statement:endTime.Subtract(startTime).Minutes;will always return (minutesDifference % 60). Obviously which is not desired when we are only talking about minutes (not hours here).
Here are some of the ways if you want to get total number of minutes(in different typecasts):
如果 endTime 和 startTime 之间的差值大于或等于 60 Minutes,则语句:endTime.Subtract(startTime).Minutes;将始终返回(minutesDifference % 60)。显然,当我们只谈论分钟(这里不是小时)时,这是不希望的。
如果您想获得total number of minutes(在不同的类型转换中),这里有一些方法:
// Default value that is returned is of type *double*
double double_minutes = endTime.Subtract(startTime).TotalMinutes;
int integer_minutes = (int)endTime.Subtract(startTime).TotalMinutes;
long long_minutes = (long)endTime.Subtract(startTime).TotalMinutes;
string string_minutes = (string)endTime.Subtract(startTime).TotalMinutes;

